当前位置: 代码迷 >> Sql Server >> 相连日期组合为一条记录,不连的日期单独一条,该怎么处理
  详细解决方案

相连日期组合为一条记录,不连的日期单独一条,该怎么处理

热度:102   发布时间:2016-04-27 13:59:11.0
相连日期组合为一条记录,不连的日期单独一条
请假信息:
---表结构 tb
ID badge name date
1 10066 张立英 2011-08-01 
2 10066 张立英 2011-08-02 
3 10066 张立英 2011-08-03 
4 10066 张立英 2011-08-04 
5 10066 张立英 2011-08-05 
6 10066 张立英 2011-08-06 
7 10066 张立英 2011-08-07 
8 10066 张立英 2011-08-08 
9 10066 张立英 2011-08-09 
10 10066 张立英 2011-08-10 
11 10070 戴开忠 2011-08-07 
12 10070 戴开忠 2011-08-13 
13 10070 戴开忠 2011-08-14 
14 10070 戴开忠 2011-08-15 
15 10070 戴开忠 2011-08-16 
16 10070 戴开忠 2011-08-17 
17 10070 戴开忠 2011-08-18 
18 10075 成定才 2011-08-06 
22 10109 吴伦秀 2011-08-01 
24 10109 吴伦秀 2011-08-13 
25 10109 吴伦秀 2011-08-14 
26 10109 吴伦秀 2011-08-16 


想要结果:

badge name begindate enddate
10066 张立英 2011-08-01 2011-08-10
10070 戴开忠 2011-08-07 2011-08-07
10070 戴开忠 2011-08-13 2011-08-18
10075 成定才 2011-08-06 2011-08-06
10109 吴伦秀 2011-08-01 2011-08-01
10109 吴伦秀 2011-08-13 2011-08-14 
10109 吴伦秀 2011-08-16 2011-08-16

在线等各位大侠

------解决方案--------------------
SQL code
declare @tb table ([ID] int,[badge] int,[name] varchar(6),[date] datetime)insert @tbselect 1,10066,'张立英','2011-08-01' union allselect 2,10066,'张立英','2011-08-02' union allselect 3,10066,'张立英','2011-08-03' union allselect 4,10066,'张立英','2011-08-04' union allselect 5,10066,'张立英','2011-08-05' union allselect 6,10066,'张立英','2011-08-06' union allselect 7,10066,'张立英','2011-08-07' union allselect 8,10066,'张立英','2011-08-08' union allselect 9,10066,'张立英','2011-08-09' union allselect 10,10066,'张立英','2011-08-10' union allselect 11,10070,'戴开忠','2011-08-07' union allselect 12,10070,'戴开忠','2011-08-13' union allselect 13,10070,'戴开忠','2011-08-14' union allselect 14,10070,'戴开忠','2011-08-15' union allselect 15,10070,'戴开忠','2011-08-16' union allselect 16,10070,'戴开忠','2011-08-17' union allselect 17,10070,'戴开忠','2011-08-18' union allselect 18,10075,'成定才','2011-08-06' union allselect 22,10109,'吴伦秀','2011-08-01' union allselect 24,10109,'吴伦秀','2011-08-13' union allselect 25,10109,'吴伦秀','2011-08-14' union allselect 26,10109,'吴伦秀','2011-08-16'select a.*,b.date  from (select row_number() over(order by t.badge)as rn, t.badge,t.name,date from @tb twhere not exists (select 1 from @tb where badge=t.badge and datediff(dd,t.date,date)=-1))as a join (select row_number() over(order by t.badge)as rn, badge,date from @tb twhere not exists (select 1 from @tb where badge=t.badge and datediff(dd,t.date,date)=1))as bon a.badge=b.badge and a.rn =b.rn /*rn    badge    name    date    date1    10066    张立英    2011-08-01 00:00:00.000    2011-08-10 00:00:00.0002    10070    戴开忠    2011-08-07 00:00:00.000    2011-08-07 00:00:00.0003    10070    戴开忠    2011-08-13 00:00:00.000    2011-08-18 00:00:00.0004    10075    成定才    2011-08-06 00:00:00.000    2011-08-06 00:00:00.0005    10109    吴伦秀    2011-08-01 00:00:00.000    2011-08-01 00:00:00.0006    10109    吴伦秀    2011-08-13 00:00:00.000    2011-08-14 00:00:00.0007    10109    吴伦秀    2011-08-16 00:00:00.000    2011-08-16 00:00:00.000*/
  相关解决方案