This commit is contained in:
2021-12-16 11:56:03 -06:00
parent df36291b7e
commit 424e0865c2
7 changed files with 333 additions and 124 deletions

64
sql/get_top.sql Normal file
View File

@@ -0,0 +1,64 @@
SELECT
description,
sku,
level2,
level3,
revenue,
quantity,
trend
from
(
SELECT
*,
ROW_NUMBER () OVER (
PARTITION BY level2
ORDER BY
SUM(revenue) DESC
) rownum,
SUM(revenue) OVER (PARTITION BY level2) total,
ROUND(SUM(revenue), 2) as revenue,
SUM(quantity) as quantity,
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 = $year
and month between $($month - 4)
and $($month - 1)
) over (PARTITION BY description) as past,
sum(quantity) FILTER (
WHERE
year = $year
and month = %[2]s
) over (PARTITION BY description) as present
from
data
)
)
where
year = %[1]s
and month = %[2]s
GROUP BY
description
ORDER BY
total DESC
)
WHERE
rownum <= 10
and revenue > 0
order by
(sum(revenue) over (
partition by level2
)) desc;