在表中为3列应用Row_Number

时间:2017-03-16 13:46:22

标签: sql sql-server row-number

我有一张名为" Temp"像这样,

ItemId  JobId   Status
1       11      Yes
1       12      No
1       12      Yes
1       13      Yes
2       12      Yes
2       12      Yes
2       11      No
2       13      Yes
2       13      No
2       13      Yes

我的预期输出是,需要根据值为这3列(ItemId,JobId,Status)设置系列。

输出应为as,

iID     iSN     JobId   JobSN   Status  statusId
1       1       11      1       Yes     1
1       2       12      1       No      1
1       3       12      2       Yes     1
1       4       13      1       Yes     1
2       1       12      1       Yes     1
2       2       12      2       Yes     2
2       3       11      1       No      1
2       4       13      1       Yes     1
2       5       13      2       No      1
2       6       13      3       Yes     2

Here iSN needs to set based on ItemId
JobSN based on JobId->ItemId
StatusId based on Status->JobId->ItemId

我知道要在列上设置系列,需要使用 ROW_NUMBER()OVER(PARTITION [ColumnName])对列进行分区...

但是,我如何申请获得预期的产出。有人可以帮我吗?

提前致谢!!!

3 个答案:

答案 0 :(得分:1)

假设您有一个可用于对行进行排序的列,您可以执行以下操作:

创建并填充样本表(在将来的问题中保存此步骤)

DECLARE @T AS TABLE
(
    id int identity(1,1),
    ItemId int,
    JobId int,
    [Status] varchar(3)
)

INSERT INTO @T VALUES
(1, 11, 'Yes'),
(1, 12, 'No'),
(1, 12, 'Yes'),
(1, 13, 'Yes'),
(2, 12, 'Yes'),
(2, 12, 'Yes'),
(2, 11, 'No'),
(2, 13, 'Yes'),
(2, 13, 'No'),
(2, 13, 'Yes')

查询:

SELECT  ItemId As iID,     
        ROW_NUMBER() OVER(PARTITION BY ItemId ORDER BY id) As iSN,
        JobId,
        ROW_NUMBER() OVER(PARTITION BY JobId, ItemId ORDER BY id) As JobSN,
        [Status],
        ROW_NUMBER() OVER(PARTITION BY [Status],JobId, ItemId ORDER BY id) As statusId
FROM @T
ORDER BY id

结果:

iID     iSN     JobId   JobSN   Status  statusId
1       1       11      1       Yes     1
1       2       12      1       No      1
1       3       12      2       Yes     1
1       4       13      1       Yes     1
2       1       12      1       Yes     1
2       2       12      2       Yes     2
2       3       11      1       No      1
2       4       13      1       Yes     1
2       5       13      2       No      1
2       6       13      3       Yes     2

答案 1 :(得分:0)

我不清楚只有statusId列。希望这可以成为一个开始。

SELECT ItemId
    , ROW_NUMBER() OVER (PARTITION BY ItemID ORDER BY ItemID) AS iSN
    , JobId
    , ROW_NUMBER() OVER (PARTITION BY JobId, ItemID ORDER BY ItemId) AS iSN
    , Status
FROM @T 
ORDER BY ItemId

结果

+------+----+------+----+-------+
|ItemId|iSN |JobId |iSN |Status |
+------+----+------+----+-------+
|1     |1   |11    |1   |Yes
|1     |2   |12    |1   |No
|1     |3   |12    |2   |Yes
|1     |4   |13    |1   |Yes
|2     |1   |11    |1   |No
|2     |2   |12    |1   |Yes
|2     |3   |12    |2   |Yes
|2     |4   |13    |1   |Yes
|2     |5   |13    |2   |No
|2     |6   |13    |3   |Yes
+------+----+------+----+-------+

答案 2 :(得分:0)

试试这个:

SELECT Itemid AS iID
, ROW_NUMBER() OVER (PARTITION BY ITEMID  ORDER BY JobID, STATUS) AS iSN
, JobID
, ROW_NUMBER() OVER (PARTITION BY ITEMID,JobID  ORDER BY JobID, STATUS) AS iSN
, Status
, ROW_NUMBER() OVER (PARTITION BY ITEMID,JobID,STATUS  ORDER BY JobID, STATUS) AS statusid
FROM TEMP;

但你必须决定ORDER BY规则以获得总是相同的结果:我的与你的略有不同)

iID         iSN                  JobID       iSN                  Status statusid
----------- -------------------- ----------- -------------------- ------ --------------------
1           1                    11          1                    Yes    1
1           2                    12          1                    No     1
1           3                    12          2                    Yes    1
1           4                    13          1                    Yes    1
2           1                    11          1                    No     1
2           2                    12          1                    Yes    1
2           3                    12          2                    Yes    2
2           4                    13          1                    No     1
2           5                    13          2                    Yes    1
2           6                    13          3                    Yes    2