This commit is contained in:
2021-10-18 10:19:17 -05:00
commit fed464e87e
8 changed files with 238 additions and 0 deletions

68
top.sql Normal file
View File

@@ -0,0 +1,68 @@
SELECT
description,
sku,
level2,
level3,
rev,
qty,
trend,
sum(rev) over (
partition by level2
) theorder
from
(
SELECT
ROW_NUMBER () OVER (
PARTITION BY level2
ORDER BY
SUM(revenue) DESC
) rownum,
SUM(revenue) OVER (PARTITION BY level2) total,
description,
sku,
level2,
level3,
ROUND(SUM(revenue), 2) as rev,
SUM(quantity) as qty,
trend
FROM
(
select
*,
cast(past as float) / 3 as past,
round(
cast(present as float) /(cast(past as float) / 3.0),
2
) as trend
from
(
select
*,
sum(quantity) FILTER (
WHERE
year = 2021
and month between 6
and 8
) over (PARTITION BY description) as past,
sum(quantity) FILTER (
WHERE
year = 2021
and month = 9
) over (PARTITION BY description) as present
from
data
)
)
where
year = 2021
and month = 9
GROUP BY
description
ORDER BY
total DESC
)
WHERE
rownum <= 10
and rev > 0
order by
theorder desc;