求sql:按相同日期分组并显示组号

吃饱饱吖 2013-10-29 03:30:24
如题:



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

*/
...全文
421 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
吃饱饱吖 2013-10-29
  • 打赏
  • 举报
回复
谢谢各位,问题解决了,同时也学习到了知识,结贴啦:D
LongRui888 2013-10-29
  • 打赏
  • 举报
回复
通过dense_rank函数,进行密集的排名,就可以实现的:


if object_id('tb') is not null
   drop table tb
go
create table tb(s_time datetime,na varchar(20))
 
insert tb
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,
       convert(varchar(10),s_time,120) as s_time,
       na 
from tb
/*
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
*/
Andy__Huang 2013-10-29
  • 打赏
  • 举报
回复
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 group_no=(select count(distinct s_time) from #t b where b.s_time<=a.s_time),s_time,na
from #t a


/*
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

*/
發糞塗牆 2013-10-29
  • 打赏
  • 举报
回复
 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
chen357313771 2013-10-29
  • 打赏
  • 举报
回复

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

*/
-Tracy-McGrady- 2013-10-29
  • 打赏
  • 举报
回复

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
,convert(char(10),s_time,120) as s_time,na 
from #t

drop table #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

(6 行受影响)

-Tracy-McGrady- 2013-10-29
  • 打赏
  • 举报
回复

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 行受影响)

22,300

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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