我正在使用Amazon Redshift查询数据。我想选择在每个季度组中产生最多收入的前4个产品。我使用windows函数计算一个季度内每个产品的收入总和值,然后使用另一个窗口函数来获取每个组中的前4个项目。这是我的代码:
SELECT es.itemid,es.total_Revenue,es.quarter
FROM (
SELECT SALE_CET.itemid
--get total revenue per quarter
,SUM(SALE_CET.totalseller) OVER
(PARTITION BY SALE_CET.itemid,SALE_CET.quarter) AS total_Revenue
,SALE_CET.quarter
--Rank the data by total revenue within each group
,ROW_NUMBER() OVER
(PARTITION BY SALE_CET.quarter ORDER BY total_Revenue DESC
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS ra
FROM SALE_CET) es
WHERE es.ra <=4
我知道我们可能无法在order-by子句中使用聚合值。但是,如果我想按季度每个项目的总收入订购记录,我该如何实施呢?
感谢您的帮助。
SALE_CET表是从CTE创建的临时表。 Create table命令将类似于:
WITH SALE_CET (userid,itemid, totalseller,year,month,quarter,dayofyear) AS
( SELECT userid
,itemid
,itemprice*orderqty AS TotalSeller
,DATEPART (year, orderdatetime) AS year
,DATEPART (month, orderdatetime) AS month
,DATEPART (quarter, orderdatetime) AS quarter
,DATEPART (dayofyear, orderdatetime) AS dayofyear
FROM sales)
答案 0 :(得分:0)
如果您在子查询中按收入降序添加另一个ROW_NUMBER()或DENSE_RANK(),那么您应该能够按外部查询的顺序访问该值。
如下所示。这是一般的想法。
SELECT
*
FROM
(
SELECT
itemid,
total_Revenue,
quarter
ra,
RevenueByItemThenByQuarter=DENSE_RANK() OVER
(PARTITION BY SALE_CTE2,ietemid,SALE_CET2.quarter ORDER BY SALE_CTE2.total_Revenue DESC
FROM
(
SELECT SALE_CET.itemid
--get total revenue per quarter
,SUM(SALE_CET.totalseller) OVER
(PARTITION BY SALE_CET.itemid,SALE_CET.quarter) AS total_Revenue
,SALE_CET.quarter
--Rank the data by total revenue within each group
,ROW_NUMBER() OVER
(PARTITION BY SALE_CET.quarter ORDER BY total_Revenue DESC
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS ra
FROM SALE_CET
)AS SALE_CET2
)AS es
WHERE
es.ra <=4
ORDER BY
es.RevenueByItemThenByQuarter