求sql 增加一个编号列

hzg98 2018-01-15 07:45:53
已知
;with tb(列1,列2) as(
select 'a',1 union all
select 'a',1 union all
select'b',1 union all
select 'b',1 union all
select 'c',2 union all
select 'c',2 union all
select 'd',2 union all
select 'd',2 union all
select 'e',2 union all
select 'e',2
)
select * from tb


列1	列2
a 1
a 1
b 1
b 1
c 2
c 2
d 2
d 2
e 2
e 2


要求列1,列2完全不变,根据列1的不同,增加一个编号列,谢谢!!
列1	列2  	编号
a 1 1
a 1 1
b 1 2
b 1 2
c 2 3
c 2 3
d 2 4
d 2 4
e 2 5
e 2 5
...全文
893 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
中国风 2018-01-25
  • 打赏
  • 举报
回复
看数据,应该按列2排序,再列1默认排序的先后 如:列1两个a隔开时,是否应该取a排序最小的值 如下例子排序为? /* 列1 列2 b 1 a 1 b 1 a 1 */
OwenZeng_DBA 2018-01-25
  • 打赏
  • 举报
回复
引用 10 楼 roy_88 的回复:
不文明用语,别引用 关1周让其反省,影响大家讨论气氛
感觉大版,,,
中国风 2018-01-25
  • 打赏
  • 举报
回复
不文明用语,别引用
关1周让其反省,影响大家讨论气氛
卖水果的net 版主 2018-01-25
  • 打赏
  • 举报
回复
引用 6 楼 lstcyzj 的回复:


典型的用词不当,建议发一下自己对这个问题的理解和 解决方法吧。
OwenZeng_DBA 2018-01-25
  • 打赏
  • 举报
回复
引用 6 楼 lstcyzj 的回复:

@中国风 是不是给个小黑屋套餐,,,
吉普赛的歌 版主 2018-01-25
  • 打赏
  • 举报
回复
引用 6 楼 lstcyzj 的回复:

大家都是这么过来的, 注意文明吧
xiaoxiangqing 2018-01-16
  • 打赏
  • 举报
回复
楼上的就可以
ChinaITOldMan 2018-01-16
  • 打赏
  • 举报
回复
开窗函数 --测试数据 ;with tb(列1,列2) as( select 'a',1 union all select 'a',1 union all select'b',1 union all select 'b',1 union all select 'c',2 union all select 'c',2 union all select 'd',2 union all select 'd',2 union all select 'e',2 union all select 'e',2 ) select *,DENSE_RANK() OVER ( ORDER BY 列1, 列2 ) 编号 from tb
hzg98 2018-01-15
  • 打赏
  • 举报
回复
引用 2 楼 yenange 的回复:
;with tb(列1,列2) as(
select 'a',1 union all
select 'a',1 union all
select'b',1 union all
select 'b',1 union all
select 'c',2 union all
select 'c',2 union all
select 'd',2 union all
select 'd',2 union all
select 'e',2 union all
select 'e',2
)
SELECT 
a.*
,b.rid AS [序号]
from tb AS a INNER JOIN (
	SELECT ROW_NUMBER() OVER (ORDER BY 列1) as rid,列1 FROM tb GROUP BY 列1	
) AS b ON a.列1=b.列1
/*
列1   列2          序号
---- ----------- --------------------
a    1           1
a    1           1
b    1           2
b    1           2
c    2           3
c    2           3
d    2           4
d    2           4
e    2           5
e    2           5
*/
不是很通用,比如下面这个就不适用
;with tb(列1,列2) as(
select 'e',1 union all
select 'e',1 union all
select'b',1 union all
select 'b',1 union all
select 'c',2 union all
select 'c',2 union all
select 'a',2 union all
select 'a',2 union all
select 'e',2 union all
select 'e',2
)
select * from tb
吉普赛的歌 版主 2018-01-15
  • 打赏
  • 举报
回复
;with tb(列1,列2) as(
select 'a',1 union all
select 'a',1 union all
select'b',1 union all
select 'b',1 union all
select 'c',2 union all
select 'c',2 union all
select 'd',2 union all
select 'd',2 union all
select 'e',2 union all
select 'e',2
)
SELECT 
a.*
,b.rid AS [序号]
from tb AS a INNER JOIN (
	SELECT ROW_NUMBER() OVER (ORDER BY 列1) as rid,列1 FROM tb GROUP BY 列1	
) AS b ON a.列1=b.列1
/*
列1   列2          序号
---- ----------- --------------------
a    1           1
a    1           1
b    1           2
b    1           2
c    2           3
c    2           3
d    2           4
d    2           4
e    2           5
e    2           5
*/
二月十六 版主 2018-01-15
  • 打赏
  • 举报
回复
--测试数据
;with tb(列1,列2) as(
select 'a',1 union all
select 'a',1 union all
select'b',1 union all
select 'b',1 union all
select 'c',2 union all
select 'c',2 union all
select 'd',2 union all
select 'd',2 union all
select 'e',2 union all
select 'e',2
)
select *,DENSE_RANK() OVER ( ORDER BY 列1, 列2 ) 编号 from tb





34,576

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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