我试图有3个月的报告(第7-9个月),根据他们的"超额目标"(或销售)的总和显示每个月的前3个产品。这是我的数据(这个只显示第27周)
select * from MV_CUSTOMER_ORDERSR27 s
where s.BRANCH_ID = 1
and s.WEEK_NUMBER_OVERALL = 27;
YEAR MONTH_NUMBER_OVERALL WEEK_NUMBER_OVERALL REGION_ID PRODUCT_ID BRANCH_ID OVERTARGET
---------- -------------------- ------------------- ---------- ---------- ---------- ----------
2014 7 27 1 1 1 30
2014 7 27 1 2 1 11
2014 7 27 1 3 1 14
2014 7 27 1 4 1 1
2014 7 27 1 5 1 7
2014 7 27 1 1 1 -20
2014 7 27 1 2 1 19
2014 7 27 1 3 1 -26
2014 7 27 1 4 1 5
2014 7 27 1 5 1 12
2014 7 27 1 1 1 -30
2014 7 27 1 2 1 -21
2014 7 27 1 3 1 3
2014 7 27 1 4 1 20
2014 7 27 1 5 1 -14
2014 7 27 1 1 1 19
2014 7 27 1 2 1 -17
2014 7 27 1 3 1 2
2014 7 27 1 4 1 -16
2014 7 27 1 5 1 22
2014 7 27 1 1 1 -29
2014 7 27 1 2 1 2
2014 7 27 1 3 1 3
2014 7 27 1 4 1 -29
2014 7 27 1 5 1 27
我一直想尝试这样的事情
YEAR MONTH_NUMBER_OVERALL WEEK_NUMBER_OVERALL REGION_ID PRODUCT_ID OVERTARGET
---------- -------------------- ------------------- ---------- ---------- ----------
2014 7 1 1 5 21
2014 7 1 1 3 18
2014 7 1 1 2 9
2014 8 1 1 1 20
2014 8 1 1 2 11
2014 8 1 1 4 6
2014 9 1 1 3 18
2014 9 1 1 2 13
2014 9 1 1 5 5
谢谢你的帮助。
答案 0 :(得分:2)
也许有帮助:
SELECT year,
month_number_overall,
product_id,
overtarget
FROM(SELECT year,
month_number_overall,
product_id,
overtarget,
ROW_NUMBER() OVER (PARTITION BY year, month_number_overall
ORDER BY overtarget DESC) AS rank
FROM(SELECT year,
month_number_overall,
product_id,
SUM(overtarget) AS overtarget
FROM yourtable
WHERE year = 2014
AND month_number_overall BETWEEN 7 AND 9
GROUP
BY year,
month_number_overall,
product_id
)
)
WHERE rank < 4
ORDER
BY 1, 2, 4 DESC