【发布时间】:2015-12-17 11:35:40
【问题描述】:
我想根据上面一行中的值给值一个数字,这样当序列中断时,它应该再次从 1 开始,否则应该继续增加数字。
查询是:
select'30300001' as lst union all
select'30300002' union all
select'30300003' union all
select'30300004' union all
select'30300001' union all
select'30300006' union all
select'30300007' union all
select'30300008' union all
select'30300009'
我想要的输出是:
select'30300001' as lst,1 as rnk union all
select'30300002',2 union all
select'30300003',3 union all
select'30300004',4 union all
select'30300001',1 union all
select'30300006',1 union all
select'30300007',2 union all
select'30300008',3 union all
select'30300009',4
我尝试使用 row_number 和 rank 函数,但无法获得所需的输出。我怎样才能得到想要的结果?
【问题讨论】:
-
您使用的是哪个 dbms?
-
@jarlh 它的 SQL Server 2008
-
SQL 表和结果集代表 无序 集。您需要一列来指定排序。行的顺序是如何定义的?
-
我列出的订单是基于 ID 列的排序,我没有在这个查询中将其作为问我问题的最小的 sn-p。
标签: sql sql-server sql-server-2008 tsql