Files
xlp/get_top.py
2022-03-18 10:00:54 -05:00

75 lines
1.7 KiB
Python

get_top = """
SELECT
description,
sku,
level2,
level3,
rev,
qty,
tren
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 rev,
SUM(quantity) as qty,
tren
FROM
(
select
*,
cast(past as float) / 3 as past,
round(
cast(present as float) /(cast(past as float) / 3.0),
2
) as tren
from
(
select
*,
sum(quantity) FILTER (
WHERE
year = {0}
and month between {1}
and {2}
) over (PARTITION BY description) as past,
sum(quantity) FILTER (
WHERE
year = {3}
and month = {4}
) over (PARTITION BY description) as present
from
data
)
)
where
year = {3}
and month = {4}
GROUP BY
description
ORDER BY
total DESC
)
WHERE
rownum <= 10
and revenue > 0
order by
(sum(revenue) over (
partition by level2
)) desc;
""".format(2022,1,2,2022,3)
import sqlite3
conn = sqlite3.connect("data.db")
res = conn.execute(get_top)
print(res.description)
for row in res.fetchall():
print(row)