相连日期组合为一条记录,不连的日期单独一条

chenyukuai 2012-03-09 02:49:48
请假信息:
---表结构 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

在线等各位大侠
...全文
132 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
chenyukuai 2012-03-12
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 zhaowenzhong 的回复:]
SQL code

declare @tb table (
[ID] int,
[badge] int,
[name] varchar(6),
[date] datetime
)
insert @tb
select 1,10066,'张立英','2011-08-01' union all
select 2,10066,'张立英','2011-08-02' union all
……
[/Quote]
非常感谢zhaowenzhong
这是我想要的结果!
黄_瓜 2012-03-09
  • 打赏
  • 举报
回复
晚上 看看。
东海凌波 2012-03-09
  • 打赏
  • 举报
回复
select a.badge, a.name, min(a.date),b.date from tb a
left join (select max(date) from tb group by ID) b on a.ID=b.ID
group by a.badge, a.name,b.date,a.ID
Felixzhaowenzhong 2012-03-09
  • 打赏
  • 举报
回复

declare @tb table (
[ID] int,
[badge] int,
[name] varchar(6),
[date] datetime
)
insert @tb
select 1,10066,'张立英','2011-08-01' union all
select 2,10066,'张立英','2011-08-02' union all
select 3,10066,'张立英','2011-08-03' union all
select 4,10066,'张立英','2011-08-04' union all
select 5,10066,'张立英','2011-08-05' union all
select 6,10066,'张立英','2011-08-06' union all
select 7,10066,'张立英','2011-08-07' union all
select 8,10066,'张立英','2011-08-08' union all
select 9,10066,'张立英','2011-08-09' union all
select 10,10066,'张立英','2011-08-10' union all
select 11,10070,'戴开忠','2011-08-07' union all
select 12,10070,'戴开忠','2011-08-13' union all
select 13,10070,'戴开忠','2011-08-14' union all
select 14,10070,'戴开忠','2011-08-15' union all
select 15,10070,'戴开忠','2011-08-16' union all
select 16,10070,'戴开忠','2011-08-17' union all
select 17,10070,'戴开忠','2011-08-18' union all
select 18,10075,'成定才','2011-08-06' union all
select 22,10109,'吴伦秀','2011-08-01' union all
select 24,10109,'吴伦秀','2011-08-13' union all
select 25,10109,'吴伦秀','2011-08-14' union all
select 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 t
where 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 t
where not exists (select 1 from @tb where badge=t.badge and datediff(dd,t.date,date)=1))as b
on a.badge=b.badge and a.rn =b.rn
/*
rn badge name date date
1 10066 张立英 2011-08-01 00:00:00.000 2011-08-10 00:00:00.000
2 10070 戴开忠 2011-08-07 00:00:00.000 2011-08-07 00:00:00.000
3 10070 戴开忠 2011-08-13 00:00:00.000 2011-08-18 00:00:00.000
4 10075 成定才 2011-08-06 00:00:00.000 2011-08-06 00:00:00.000
5 10109 吴伦秀 2011-08-01 00:00:00.000 2011-08-01 00:00:00.000
6 10109 吴伦秀 2011-08-13 00:00:00.000 2011-08-14 00:00:00.000
7 10109 吴伦秀 2011-08-16 00:00:00.000 2011-08-16 00:00:00.000

*/
chenyukuai 2012-03-09
  • 打赏
  • 举报
回复
也不对
Felixzhaowenzhong 2012-03-09
  • 打赏
  • 举报
回复
declare @tb table (
[ID] int,
[badge] int,
[name] varchar(6),
[date] datetime
)
insert @tb
select 1,10066,'张立英','2011-08-01' union all
select 2,10066,'张立英','2011-08-02' union all
select 3,10066,'张立英','2011-08-03' union all
select 4,10066,'张立英','2011-08-04' union all
select 5,10066,'张立英','2011-08-05' union all
select 6,10066,'张立英','2011-08-06' union all
select 7,10066,'张立英','2011-08-07' union all
select 8,10066,'张立英','2011-08-08' union all
select 9,10066,'张立英','2011-08-09' union all
select 10,10066,'张立英','2011-08-10' union all
select 11,10070,'戴开忠','2011-08-07' union all
select 12,10070,'戴开忠','2011-08-13' union all
select 13,10070,'戴开忠','2011-08-14' union all
select 14,10070,'戴开忠','2011-08-15' union all
select 15,10070,'戴开忠','2011-08-16' union all
select 16,10070,'戴开忠','2011-08-17' union all
select 17,10070,'戴开忠','2011-08-18' union all
select 18,10075,'成定才','2011-08-06' union all
select 22,10109,'吴伦秀','2011-08-01' union all
select 24,10109,'吴伦秀','2011-08-13' union all
select 25,10109,'吴伦秀','2011-08-14' union all
select 26,10109,'吴伦秀','2011-08-16'

select t.badge,b.name,MIN(t.date)as begindate,MAX(b.date)as enddate from @tb t,@tb b where t.badge=b.badge
group by t.badge,b.name

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

是要这种结果吗
chenyukuai 2012-03-09
  • 打赏
  • 举报
回复
日期相连的就取相连的最小日期作为开始日期,相连的最大日期作为结束日期,
日期不相连的就取本来的日期作为开始日期和结束日期
chenyukuai 2012-03-09
  • 打赏
  • 举报
回复
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
chenyukuai 2012-03-09
  • 打赏
  • 举报
回复
我要取出来的是一个开始日期,一个结束日期
AcHerat 2012-03-09
  • 打赏
  • 举报
回复

select *
from tb t
where not exists (select 1 from tb where badge=t.badge and datediff(dd,t.date,date)=1)

/*******************

ID badge name date
----------- ----------- ------ -----------------------
10 10066 张立英 2011-08-10 00:00:00.000
11 10070 戴开忠 2011-08-07 00:00:00.000
17 10070 戴开忠 2011-08-18 00:00:00.000
18 10075 成定才 2011-08-06 00:00:00.000
22 10109 吴伦秀 2011-08-01 00:00:00.000
25 10109 吴伦秀 2011-08-14 00:00:00.000
26 10109 吴伦秀 2011-08-16 00:00:00.000

(7 行受影响)
  • 打赏
  • 举报
回复

--> 测试数据:[tb]
go
if object_id('[tb]') is not null
drop table [tb]
go
create table [tb](
[ID] int,
[badge] int,
[name] varchar(6),
[date] datetime
)
go
insert [tb]
select 1,10066,'张立英','2011-08-01' union all
select 2,10066,'张立英','2011-08-02' union all
select 3,10066,'张立英','2011-08-03' union all
select 4,10066,'张立英','2011-08-04' union all
select 5,10066,'张立英','2011-08-05' union all
select 6,10066,'张立英','2011-08-06' union all
select 7,10066,'张立英','2011-08-07' union all
select 8,10066,'张立英','2011-08-08' union all
select 9,10066,'张立英','2011-08-09' union all
select 10,10066,'张立英','2011-08-10' union all
select 11,10070,'戴开忠','2011-08-07' union all
select 12,10070,'戴开忠','2011-08-13' union all
select 13,10070,'戴开忠','2011-08-14' union all
select 14,10070,'戴开忠','2011-08-15' union all
select 15,10070,'戴开忠','2011-08-16' union all
select 16,10070,'戴开忠','2011-08-17' union all
select 17,10070,'戴开忠','2011-08-18' union all
select 18,10075,'成定才','2011-08-06' union all
select 22,10109,'吴伦秀','2011-08-01' union all
select 24,10109,'吴伦秀','2011-08-13' union all
select 25,10109,'吴伦秀','2011-08-14' union all
select 26,10109,'吴伦秀','2011-08-16'



select * from tb a
where [date]=(select max([date]) from tb b
where a.badge =b.badge )
union all
select ID,[badge],[name],[date] from tb a
where a.[date] not in
(select dateadd(dd,1,[date]) from tb b where a.[badge]=b.[badge])
and a.[date] not in (select dateadd(dd,-1,[date]) from tb b
where a.[badge]=b.[badge])
order by badge


/*
ID badge name date
26 10109 吴伦秀 2011-08-16 00:00:00.000
18 10075 成定才 2011-08-06 00:00:00.000
17 10070 戴开忠 2011-08-18 00:00:00.000
10 10066 张立英 2011-08-10 00:00:00.000
11 10070 戴开忠 2011-08-07 00:00:00.000
18 10075 成定才 2011-08-06 00:00:00.000
22 10109 吴伦秀 2011-08-01 00:00:00.000
26 10109 吴伦秀 2011-08-16 00:00:00.000
*/
chenyukuai 2012-03-09
  • 打赏
  • 举报
回复
各位大侠帮帮忙啊,小弟在此等候。
  • 打赏
  • 举报
回复
看错了,楼上作废
chenyukuai 2012-03-09
  • 打赏
  • 举报
回复
你这样子查出来的是日期最大的那条数据
  • 打赏
  • 举报
回复
select * from tbl a
where date=(select max(date) from tbl b
where a.badge =b.badge )

27,580

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧