SQL 对重复组进行排名

SQL ranking repeating groups

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

然后我想 select 这个数据不带日期和按部分、行、产品和排名分组。

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

更新----

使用以下内容:

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) 上的产品相同的情况下,我希望它们具有相同的等级。

这实际上与您的其他 相似。不同的是每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 语句中。

需要更正分区中的顺序。

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;