如何使用带有2列的行号设置相同的序列号

时间:2017-02-04 03:44:48

标签: sql sql-server

我有下表和值,我希望输出如下预期输出

declare @TestData table (ID int, sku char(6), product varchar(15))
insert into @TestData values (1 ,  'ABDE01'      ,'SPA')
insert into @TestData values (2 ,  'ABDE01'      ,'GRE')
insert into @TestData values (3 ,  'ABDE01'      ,'RUS')
insert into @TestData values (2 ,  'ABAD03'      ,'SPA')
insert into @TestData values (4 ,  'ABAD03'      ,'SPA')
insert into @TestData values (6 ,  'ABAD03'      ,'SPA')
insert into @TestData values (1 ,  'ABAD02'      ,'SPA')
insert into @TestData values (2 ,  'ABAD02'      ,'SPA')
insert into @TestData values (4 ,  'ABAD05'      ,'ENG')

预期输出为:

SKU Product NewRow
ABAD02  SPA 1
ABAD02  SPA 1
ABAD02  ENG 2
ABAD03  SPA 1
ABAD03  SPA 1
ABAD03  SPA 1
ABDE01  SPA 1
ABDE01  GRE 2
ABDE01  RUS 3

我尝试了以下查询,但它没有按预期工作,它给了我与我想要的不同

declare @TestData table (ID int, sku char(6), product varchar(15))
insert into @TestData values (1 ,  'ABDE01'      ,'SPA')
insert into @TestData values (2 ,  'ABDE01'      ,'GRE')
insert into @TestData values (3 ,  'ABDE01'      ,'RUS')
insert into @TestData values (2 ,  'ABAD03'      ,'SPA')
insert into @TestData values (4 ,  'ABAD03'      ,'SPA')
insert into @TestData values (6 ,  'ABAD03'      ,'SPA')
insert into @TestData values (1 ,  'ABAD02'      ,'SPA')
insert into @TestData values (2 ,  'ABAD02'      ,'SPA')
insert into @TestData values (4 ,  'ABAD02'      ,'ENG')


SELECT
    dt.SKU, dt.Product,  NewRow
    FROM (SELECT
               SKU, Product, --ROW_NUMBER() OVER (PARTITION BY product ORDER BY product) AS RowID,
               DENSE_RANK() OVER (PARTITION BY sku,product ORDER BY id) as NewRow 
              FROM @TestData              
         ) AS dt

但我得到的结果如下:

SKU Product NewRow
ABAD02  ENG 1
ABAD02  SPA 1
ABAD02  SPA 2
ABAD03  SPA 1
ABAD03  SPA 2
ABAD03  SPA 3
ABDE01  GRE 1
ABDE01  RUS 1
ABDE01  SPA 1

请告知我错过了什么。谢谢!

3 个答案:

答案 0 :(得分:1)

您不需要res = Model1.joins(:items).where(items: {id: [1, 2, 3]}).uniq ,只需要PARTITION BY

ORDER BY

如果您只想让sku / product组合具有不同的值,则不需要子查询:

SELECT dt.SKU, dt.Product,  
       DENSE_RANK() OVER (ORDER BY min_id) as NewRow 
FROM (SELECT td.*,
             MIN(td.id) OVER (PARTITION BY sku, product) as min_id
      FROM @TestData td
     ) td;

答案 1 :(得分:0)

是否可能有以下样本数据?

    insert into @TestData values (1 ,  'ABAD02'      ,'SPA')
    insert into @TestData values (5 ,  'ABAD02'      ,'SPA')
    insert into @TestData values (4 ,  'ABAD02'      ,'ENG')

在上述情况下,如何确定SPA和ENG之间的排序?

如果您有产品分类定义列表,则最好处理NewRow,例如。

     declare @TestData table (ID int, sku char(6), product varchar(15))
    insert into @TestData values (1 ,  'ABDE01'      ,'SPA')
    insert into @TestData values (2 ,  'ABDE01'      ,'GRE')
    insert into @TestData values (3 ,  'ABDE01'      ,'RUS')
    insert into @TestData values (2 ,  'ABAD03'      ,'SPA')
    insert into @TestData values (4 ,  'ABAD03'      ,'SPA')
    insert into @TestData values (6 ,  'ABAD03'      ,'SPA')
    insert into @TestData values (1 ,  'ABAD02'      ,'SPA')
    insert into @TestData values (2 ,  'ABAD02'      ,'SPA')
    insert into @TestData values (4 ,  'ABAD02'      ,'ENG')

    SELECT *,DENSE_RANK() OVER ( PARTITION BY sku ORDER BY x.sort) as NewRow  
    FROM @TestData AS t
    -- definating the sort of product
    INNER JOIN (VALUES(1,'SPA'),(2,'GRE'),(3,'ENG'),(4,'RUS')) x(sort,prd) ON x.prd=t.product
    ORDER BY t.sku,t.ID
ID          sku    product         sort        prd  NewRow
----------- ------ --------------- ----------- ---- --------------------
1           ABAD02 SPA             1           SPA  1
2           ABAD02 SPA             1           SPA  1
4           ABAD02 ENG             3           ENG  2
2           ABAD03 SPA             1           SPA  1
4           ABAD03 SPA             1           SPA  1
6           ABAD03 SPA             1           SPA  1
1           ABDE01 SPA             1           SPA  1
2           ABDE01 GRE             2           GRE  2
3           ABDE01 RUS             4           RUS  3

答案 2 :(得分:0)

使用窗口函数最接近预期输出的是按sku划分并按产品排序:

SELECT *
, NewRow = DENSE_RANK()OVER(PARTITION BY sku ORDER BY product)
FROM @TestData
ORDER BY sku, ID

但是,您的预期输出似乎按ID进一步排序,这不是窗口函数在不拆分分组的情况下处理的事情。游标循环可能是完全匹配预期输出的唯一方法。