SQL排名重复组

时间:2016-02-12 12:07:49

标签: sql ranking

我有以下按开始日期排序的数据。我想要做的是按产品名称对产品名称进行排名,例如产品编号27675在第1行,第一次找到此产品,因此排名为1 ...再次,此产品在第5行,因此排名为2。

我想在没有日期和分组,行,产品和排名的情况下选择此数据。

SECTION_NAME    ROW_NAME    PRODUCT NAME    start_date         DESIRED RANK    
SEW             LS-1-A_1    27675           02/03/2016 14:39    1
SEW             LS-1-A_1    26298           06/04/2016 16:56    1
SEW             LS-1-A_1    26298           16/05/2016 16:56    1
SEW             LS-1-A_1    26298           04/06/2016 09:26    1
SEW             LS-1-A_1    27675           16/06/2016 16:22    2
SEW             LS-1-A_1    18969           17/07/2016 12:54    1
SEW             LS-1-A_1    26298           01/08/2016 10:52    2
SEW             LS-1-A_1    27675           29/08/2016 08:38    3
SEW             LS-1-A_1    LEVI F17 SMS    12/09/2016 13:39    1

我正在制作的排名是:

SECTION_NAME    ROW_NAME    PRODUCT NAME    start_date             RANK    
SEW             LS-1-A_1    27675           02/03/2016 14:39    1
SEW             LS-1-A_1    26298           06/04/2016 16:56    1
SEW             LS-1-A_1    26298           16/05/2016 16:56    2
SEW             LS-1-A_1    26298           04/06/2016 09:26    3
SEW             LS-1-A_1    27675           16/06/2016 16:22    2
SEW             LS-1-A_1    18969           17/07/2016 12:54    1
SEW             LS-1-A_1    26298           01/08/2016 10:52    4
SEW             LS-1-A_1    27675           29/08/2016 08:38    3
SEW             LS-1-A_1    LEVI F17 SMS    12/09/2016 13:39    1


SELECT 
   SECTION_NAME,
   ROW_NAME,
   PRODUCT_NAME,
   start_date, 
   dense_rank() OVER (PARTITION BY product_name ORDER BY START_DATE)RANK
FROM
   TABLES

Order by
    SECTION_NAME,ROW_NAME,START_DATE

UPDATE ----

使用以下内容:

SELECT 
  SECTION_NAME ,
  ROW_NAME,
  PRODUCT_NAME [Style], 
  (START_DATE) as [Start dt],
  SUM(isChange) OVER (PARTITION BY SECTION_NAME , ROW_NAME,product_name     ORDER BY START_DATE) as rank_

FROM (SELECT SECTION_NAME,
        ROW_NAME,
        PRODUCT_NAME,
        sd.START_DATE,
         (CASE WHEN lag(PR.ROW_NAME ) over (order by SD.START_DATE) = PR.ROW_NAME 
               THEN 0
               ELSE 1
          END) as IsChange
  FROM 
    TABLES_t
 ) t
Order by SECTION_NAME, ROW_NAME, START_DATE;

产生这个:(在产品在每一行(10,11,12)上相同的情况下,我希望它们具有相同的等级。

enter image description here

2 个答案:

答案 0 :(得分:2)

这实际上与您的其他question类似。不同之处在于计数需要为每个product_name重新开始。所以,这是一种方法:

SELECT PS.SECTION_NAME [Planning Group], PR.ROW_NAME as [Planning Row],
       P.PRODUCT_NAME [Style], (SD.START_DATE) as [Start dt],
       SUM(isChange) OVER (PARTITION BY product_name ORDER BY start_dt) as rank_
FROM (SELECT t.*,
             (CASE WHEN lag(row) over (order by start_dt) = row
                   THEN 0
                   ELSE 1
              END) as IsChange
      FROM tables_ t
     ) t
Order by PS.SECTION_NAME, PR.ROW_NAME, SD.START_DATE;

注意:在上一个问题中,我不了解SECTION_NAMEROW_NAME的作用。此版本忽略这些列。如果您希望查询重新开始每个组中的编号,请将它们包含在partition by语句中。

答案 1 :(得分:0)

需要通过。

更正分区中的顺序
SELECT 
SECTION_NAME ,
ROW_NAME,
PRODUCT_NAME [Style], 
(START_DATE) ,
SUM(isChange) OVER (PARTITION BY ROW_NAME,product_name ORDER BY START_DATE) as rank_
,IsChange
FROM (SELECT SECTION_NAME,
        ROW_NAME,
        PRODUCT_NAME,
        sd.START_DATE,
         (CASE WHEN lag(product_name ) over (order by SECTION_NAME, ROW_NAME, sd.START_DATE) = product_name 
               THEN 0
               ELSE 1
          END) as IsChange


  FROM 
    Table_ t
 ) t

Order by SECTION_NAME, ROW_NAME, START_DATE;

enter image description here