table
城市 去时间
上海 2001-1-1
上海 2001-4-12
上海 2001-9-1
北京 2003-11-28
北京 2003-12-30
天津 2004-4-3
----结果
城市 去时间 上一次时间 次数
上海 2001-9-1 2001-4-12 3
上海 2001-4-12 2001-1-1 2
上海 2001-1-1 null 1
北京 2003-12-30 2003-11-28 2
北京 2003-11-28 null 1
天津 2004-4-3 null 1说明:按去时间排序,生成上次去时间,次数就按时间顺序表示
城市 去时间
上海 2001-1-1
上海 2001-4-12
上海 2001-9-1
北京 2003-11-28
北京 2003-12-30
天津 2004-4-3
----结果
城市 去时间 上一次时间 次数
上海 2001-9-1 2001-4-12 3
上海 2001-4-12 2001-1-1 2
上海 2001-1-1 null 1
北京 2003-12-30 2003-11-28 2
北京 2003-11-28 null 1
天津 2004-4-3 null 1说明:按去时间排序,生成上次去时间,次数就按时间顺序表示
insert @tb
select '上海', '2001-1-1' union all
select '上海', '2001-4-12'union all
select '上海', '2001-9-1'union all
select '北京', '2003-11-28'union all
select '北京', '2003-12-30'union all
select '天津', '2004-4-3'select a.*,
(select top 1 去时间 from @tb where 城市=a.城市 and 去时间 >a.去时间 order by 去时间 ) time,(select count(1) from @tb where 城市=a.城市 and 去时间 <=a.去时间) num
from @tb a/*
(所影响的行数为 6 行)城市 去时间 time num
---------- ------------------------------------------------------ ------------------------------------------------------ -----------
上海 2001-01-01 00:00:00.000 2001-04-12 00:00:00.000 1
上海 2001-04-12 00:00:00.000 2001-09-01 00:00:00.000 2
上海 2001-09-01 00:00:00.000 NULL 3
北京 2003-11-28 00:00:00.000 2003-12-30 00:00:00.000 1
北京 2003-12-30 00:00:00.000 NULL 2
天津 2004-04-03 00:00:00.000 NULL 1(所影响的行数为 6 行)
这是你的答案:
上海 2001-01-01 00:00:00.000 2001-04-12 00:00:00.000 1
上海 2001-04-12 00:00:00.000 2001-09-01 00:00:00.000 2
上海 2001-09-01 00:00:00.000 NULL 3
北京 2003-11-28 00:00:00.000 2003-12-30 00:00:00.000 1 -----这列下次去的时间应该是null
北京 2003-12-30 00:00:00.000 NULL 2
天津 2004-04-03 00:00:00.000 NULL 1
insert @tb
select '上海', '2001-1-1' union all
select '上海', '2001-4-12'union all
select '上海', '2001-9-1'union all
select '北京', '2003-11-28'union all
select '北京', '2003-12-30'union all
select '天津', '2004-4-3'select a.*,
(select top 1 去时间 from @tb where 城市=a.城市 and 去时间 <a.去时间 order by 去时间 ) time,(select count(1) from @tb where 城市=a.城市 and 去时间 <=a.去时间) num
from @tb a(所影响的行数为 6 行)城市 去时间 time num
---------- ------------------------------------------------------ ------------------------------------------------------ -----------
上海 2001-01-01 00:00:00.000 NULL 1
上海 2001-04-12 00:00:00.000 2001-01-01 00:00:00.000 2
上海 2001-09-01 00:00:00.000 2001-01-01 00:00:00.000 3
北京 2003-11-28 00:00:00.000 NULL 1
北京 2003-12-30 00:00:00.000 2003-11-28 00:00:00.000 2
天津 2004-04-03 00:00:00.000 NULL 1(所影响的行数为 6 行)
insert @tb
select '上海', '2001-1-1' union all
select '上海', '2001-4-12'union all
select '上海', '2001-9-1'union all
select '北京', '2003-11-28'union all
select '北京', '2003-12-30'union all
select '天津', '2004-4-3'select a.*,
(select top 1 去时间 from @tb where 城市=a.城市 and 去时间 <a.去时间 order by 去时间 desc ) time,(select count(1) from @tb where 城市=a.城市 and 去时间 <=a.去时间) num
from @tb a
/*
(所影响的行数为 6 行)城市 去时间 time num
---------- ------------------------------------------------------ ------------------------------------------------------ -----------
上海 2001-01-01 00:00:00.000 NULL 1
上海 2001-04-12 00:00:00.000 2001-01-01 00:00:00.000 2
上海 2001-09-01 00:00:00.000 2001-04-12 00:00:00.000 3
北京 2003-11-28 00:00:00.000 NULL 1
北京 2003-12-30 00:00:00.000 2003-11-28 00:00:00.000 2
天津 2004-04-03 00:00:00.000 NULL 1(所影响的行数为 6 行)