Unable to rank item based on sales,

时间:2019-03-19 14:53:21

标签: mysql sql

Please help me to spot the error in the below code, I ma getting rownum as 1 for all the item. Ideally all the item should get different rownum based on Revenue

SELECT
  ITEM_NUM,
  LW_REV,
  ROW_NUMBER() over (
    PARTITION BY ITEM_NUM
    ORDER BY
      LW_OPS DESC
  ) ROWNUM
FROM
  (
    SELECT
      ITEM_NUM,
      SUM(REVENUE) AS LW_REVENUE
    FROM
      D_DAILY_SALES
    WHERE
      ACTIVITY_DAY BETWEEN TO_DATE('20190302', 'YYYYMMDD') -6
      AND TO_DATE('20190302', 'YYYYMMDD')
      AND ITEM_NUM IN (
        '1002',
        '1120',
        '1144',
        '1145',
        '1204',
        '1240',
        '1245',
        '1344',
        '1345',
        '1365',
        '1387',
        '1500',
        '1512',
        '1573',
        '1577',
        '1597',
        '1672',
        '1706',
        '1746',
        '1748',
        '1788',
        '1854',
        '1895',
        '1928'
      )
    GROUP BY
      ITEM_NUM
    ORDER BY
      LW_REV DESC
  )

Thanks in Advance!

1 个答案:

答案 0 :(得分:1)

您正在按item_num进行分区,因此每个项目都从1开始。取出分区依据-您只需要按顺序排序即可。

ROW_NUMBER() over (

    ORDER BY
      LW_OPS DESC
  )

NB。不应该按LW_revenue订购吗?