我需要行编号,其中ROW_NUMBER对于相同的值列是相同的:MFGPN(相同的MFGPN将始终按顺序).我还需要保留原始序列号.
这是我的桌子
No MFGPN 1 Z363700Z01 2 Z363700Z01 3 0119-960-1 4 1A3F1-0503-01
我尝试使用RANK()来实现所需但遇到麻烦.
SELECT RANK() OVER(ORDER BY MFGPN) As [Item], MFGPN FROM Table1 ORDER BY [No] ASC
结果
Item MFGPN Desired Result 3 Z363700Z01 1 3 Z363700Z01 1 1 0119-960-1 2 2 1A3F1-0503-01 3
感谢你们的专家建议.谢谢!
使用DENSE_RANK
窗口功能代替RANK
.Rank
当重复数据不会跳过序列Dense_Rank
.
SELECT MFGPN, Dense_rank()OVER(ORDER BY m_no) as [Desired Result] FROM (SELECT no, MFGPN, Min(no)OVER(partition BY MFGPN) AS m_no FROM (VALUES (1,'Z363700Z01' ), (2,'Z363700Z01' ), (3,'0119-960-1' ), (4,'1A3F1-0503-01')) tc (no, MFGPN))a
如果no
不是唯一的,那么DENSE_RANK
改为
Dense_rank()OVER(ORDER BY m_no,MFGPN)
结果:
+---------------+----------------+ | MFGPN | Desired Result | +---------------+----------------+ | Z363700Z01 | 1 | | Z363700Z01 | 1 | | 0119-960-1 | 2 | | 1A3F1-0503-01 | 3 | +---------------+----------------+