34,590
社区成员
发帖
与我相关
我的任务
分享
create table Table1
(keys int, startd date, endd date)
insert into Table1
select '1', '2010-01-01', '2010-12-31' union all
select '2', '2011-01-01', '2011-12-31'
select row_number() over(order by t.dates) keys,
t.dates, t.Flag
from
(select startd dates,0 Flag from Table1
union all
select endd dates,1 Flag from Table1) t
keys dates Flag
-------------------- ---------- -----------
1 2010-01-01 0
2 2010-12-31 1
3 2011-01-01 0
4 2011-12-31 1
(4 row(s) affected)
/*
Key Date Flag
-------------------- ----------------------- --------
1 2010-01-01 00:00:00.000 0(start)
2 2010-12-31 00:00:00.000 1(end)
3 2011-01-01 00:00:00.000 0(start)
4 2011-12-31 00:00:00.000 1(end)
(4 行受影响)
*/
if object_id( 'Table1') is not null
Drop Table Table1
create table Table1(
keys int
,startD datetime
,endD datetime)
insert into Table1(keys, startD, endD)
select 1, '2010-01-01', '2010-12-31' union all
select 2, '2011-01-01', '2011-12-31'
select [Key]=Row_number()OVER(ORDER BY b.keys, b.date),
[Date] = b.date,
Flag =(case
when b.aflag = 'startD' then '0(start)'
when b.aflag = 'endD' then '1(end)' end)
from Table1 a
unpivot
( date for aflag
in (startD, endD)
)as b