我有一个包含两列的表,这些列应该为itemID的组合生成一个唯一的数字,如下所示。
请帮助查询。感谢。
seq_no ItemId
-------------------
15000 AA-00001152
15000 AA-00001152
15001 AA-00001153
15002 AA-00001154
15002 AA-00001154
15003 AA-00001155
15004 AA-00001156
15004 AA-00001156
编辑:
我的输出应该是这样的uniqueNumber .. 在这里,我的unquenurber应该从15000开始并增加1,但是每个多个相同的Itemid,uniquenumber应该如下所示。
UniqueNumber | seq_no | ItemId |
+--------------+--------+-------------+
| 15000 | 15000 | AA-00001152 |
+--------------+--------+-------------+
| 15000 | 15000 | AA-00001152 |
+--------------+--------+-------------+
| 15001 | 15001 | AA-00001153 |
+--------------+--------+-------------+
| 15002 | 15002 | AA-00001154 |
+--------------+--------+-------------+
| 15002 | 15002 | AA-00001154 |
+--------------+--------+-------------+
| 15003 | 15003 | AA-00001155 |
+--------------+--------+-------------+
| 15004 | 15004 | AA-00001156 |
答案 0 :(得分:1)
如果您想为itemid
生成一个重复的唯一编号,可以将ROW_NUMBER()
与PARTITION BY ItemId
一起使用,如下所示。
这将为每个重复ItemId
declare @table table( seq_no int, ItemId varchar(100))
insert into @table
values
(15000 , 'AA-00001152'),
(15000 , 'AA-00001152'),
(15001 , 'AA-00001153'),
(15002 , 'AA-00001154'),
(15002 , 'AA-00001154'),
(15003 , 'AA-00001155'),
(15004 , 'AA-00001156'),
(15004 , 'AA-00001156')
SELECT ROW_NUMBER() OVER (PARTITION BY ItemId ORDER BY ItemId) UniqueNumber,*
FROM @table
<强>输出强>
+--------------+--------+-------------+
| UniqueNumber | seq_no | ItemId |
+--------------+--------+-------------+
| 1 | 15000 | AA-00001152 |
+--------------+--------+-------------+
| 2 | 15000 | AA-00001152 |
+--------------+--------+-------------+
| 1 | 15001 | AA-00001153 |
+--------------+--------+-------------+
| 1 | 15002 | AA-00001154 |
+--------------+--------+-------------+
| 2 | 15002 | AA-00001154 |
+--------------+--------+-------------+
| 1 | 15003 | AA-00001155 |
+--------------+--------+-------------+
| 1 | 15004 | AA-00001156 |
+--------------+--------+-------------+
| 2 | 15004 | AA-00001156 |
+--------------+--------+-------------+
如果您想在所有项目中生成唯一编号,您也可以尝试以下操作。
SELECT CAST(CAST(seq_no AS VARCHAR(10)) + CAST(UniqueNumber AS VARCHAR(10)) AS INT) UniqueNumber,seq_no,ItemId
FROM
(
SELECT ROW_NUMBER() OVER (PARTITION BY ItemId ORDER BY ItemId) UniqueNumber,*
FROM @table
) T
<强>输出强>
+--------------+--------+-------------+
| UniqueNumber | seq_no | ItemId |
+--------------+--------+-------------+
| 150001 | 15000 | AA-00001152 |
+--------------+--------+-------------+
| 150002 | 15000 | AA-00001152 |
+--------------+--------+-------------+
| 150011 | 15001 | AA-00001153 |
+--------------+--------+-------------+
| 150021 | 15002 | AA-00001154 |
+--------------+--------+-------------+
| 150022 | 15002 | AA-00001154 |
+--------------+--------+-------------+
| 150031 | 15003 | AA-00001155 |
+--------------+--------+-------------+
| 150041 | 15004 | AA-00001156 |
+--------------+--------+-------------+
| 150042 | 15004 | AA-00001156 |
+--------------+--------+-------------+
答案 1 :(得分:0)
您似乎想要dense_rank()
功能
select *,
15000+(DENSE_RANK() over (order by ItemId)-1) UniqueNumber
from table t