求sql:按雷同日期分组并显示组号
求sql:按相同日期分组并显示组号
如题:
------解决方案--------------------
------解决方案--------------------
------解决方案--------------------
------解决方案--------------------
如题:
create table #t(s_time datetime,na varchar(20))
insert #t
select '2013-10-1','aa'
union all
select '2013-10-1','bb'
union all
select '2013-10-1','cc'
union all
select '2013-10-15','dd'
union all
select '2013-10-15','ee'
union all
select '2013-10-20','ff'
select * from #t
/*
希望得到的结果
group_no s_time na
1 2013-10-01 aa
1 2013-10-01 bb
1 2013-10-01 cc
2 2013-10-15 dd
2 2013-10-15 ee
3 2013-10-20 ff
*/
sql
排序
分组
------解决方案--------------------
create table #t(s_time datetime,na varchar(20))
insert #t
select '2013-10-1','aa'
union all
select '2013-10-1','bb'
union all
select '2013-10-1','cc'
union all
select '2013-10-15','dd'
union all
select '2013-10-15','ee'
union all
select '2013-10-20','ff'
select DENSE_RANK()over(order by s_time) as group_no,* from #t
drop table #t
--
group_no s_time na
-------------------- ----------------------- --------------------
1 2013-10-01 00:00:00.000 aa
1 2013-10-01 00:00:00.000 bb
1 2013-10-01 00:00:00.000 cc
2 2013-10-15 00:00:00.000 dd
2 2013-10-15 00:00:00.000 ee
3 2013-10-20 00:00:00.000 ff
(6 行受影响)
------解决方案--------------------
create table #t(s_time datetime,na varchar(20))
insert #t
select '2013-10-1','aa'
union all
select '2013-10-1','bb'
union all
select '2013-10-1','cc'
union all
select '2013-10-15','dd'
union all
select '2013-10-15','ee'
union all
select '2013-10-20','ff'
SELECT DENSE_RANK() OVER(ORDER BY s_time) AS group_no
,CAST(s_time AS DATE) AS Date
,na
FROM #t
/*
1 2013-10-01 aa
1 2013-10-01 bb
1 2013-10-01 cc
2 2013-10-15 dd
2 2013-10-15 ee
3 2013-10-20 ff
*/
------解决方案--------------------
SELECT b.group_no ,
a.*
FROM #t a
INNER JOIN ( SELECT s_time ,
ROW_NUMBER() OVER ( ORDER BY s_time ) group_no
FROM ( SELECT DISTINCT
s_time
FROM #t
) b
) b ON a.s_time = b.s_time
------解决方案--------------------
create table #t(s_time datetime,na varchar(20))
insert #t
select '2013-10-1','aa'
union all