|
@@ -22,11 +22,6 @@ def get_where(product, unit, organic=None, limit='90 days'):
|
|
|
SQL('='),
|
|
|
Literal(product)
|
|
|
]))
|
|
|
- where.append(SQL(' ').join([
|
|
|
- Identifier('units', 'name'),
|
|
|
- SQL('='),
|
|
|
- Literal(unit),
|
|
|
- ]))
|
|
|
if organic is not None:
|
|
|
where.append(SQL(' ').join([
|
|
|
Identifier('organic'),
|
|
@@ -39,6 +34,11 @@ def get_where(product, unit, organic=None, limit='90 days'):
|
|
|
interval=SQL("{literal}::interval").format(literal=Literal(limit))
|
|
|
)
|
|
|
)
|
|
|
+ where.append(
|
|
|
+ SQL("(SELECT id FROM units WHERE name = {unit}) = conv._to").format(
|
|
|
+ unit=Literal(unit),
|
|
|
+ )
|
|
|
+ )
|
|
|
return SQL('').join([
|
|
|
SQL("WHERE"
|
|
|
"\n "),
|
|
@@ -48,6 +48,16 @@ def get_where(product, unit, organic=None, limit='90 days'):
|
|
|
def get_historic_prices_statement(sort, product, unit, organic=None, limit='90 days'):
|
|
|
partition = f"(PARTITION BY {'organic,' if organic is not None else ''} product_id, unit_id)"
|
|
|
organic_sort = f"{'organic,' if organic is not None else ''}"
|
|
|
+ _with = SQL("""WITH conv AS (
|
|
|
+ SELECT
|
|
|
+ _1._from, _1._to, factor FROM conversions AS _1
|
|
|
+ UNION
|
|
|
+ SELECT _2._to, _2._from, 1/_2.factor FROM conversions AS _2
|
|
|
+ UNION
|
|
|
+ SELECT _3._from, _3._from, 1 FROM conversions AS _3
|
|
|
+ UNION
|
|
|
+ SELECT _4.id AS _from, _4.id AS _to, 1 FROM units AS _4
|
|
|
+)""")
|
|
|
select = OrderedDict([
|
|
|
('id', Identifier('transactions','id')),
|
|
|
#('ts', SQL('{identifier}::date').format(
|
|
@@ -60,10 +70,10 @@ def get_historic_prices_statement(sort, product, unit, organic=None, limit='90 d
|
|
|
#('volume', Identifier('quantity')),
|
|
|
#('unit', Identifier('units', 'name')),
|
|
|
#('price', Identifier('price')),
|
|
|
- ('$/unit', SQL("""TRUNC(price/quantity,4)""")),
|
|
|
- ('avg', SQL(f"""TRUNC(sum(price) OVER {partition} / sum(quantity) OVER {partition}, 4)""")),
|
|
|
- ('min', SQL(f"""TRUNC(min(price/quantity) OVER {partition}, 4)""")),
|
|
|
- ('max', SQL(f"""TRUNC(max(price/quantity) OVER {partition}, 4)""")),
|
|
|
+ ('$/unit', SQL("""TRUNC(price / (quantity * factor), 4)""")),
|
|
|
+ ('avg', SQL(f"""TRUNC(sum(price) OVER {partition} / sum(quantity * factor) OVER {partition}, 4)""")),
|
|
|
+ ('min', SQL(f"""TRUNC(min(price / (quantity * factor)) OVER {partition}, 4)""")),
|
|
|
+ ('max', SQL(f"""TRUNC(max(price / (quantity * factor)) OVER {partition}, 4)""")),
|
|
|
#('total', SQL("""sum(transactions.price) OVER (PARTITION BY transactions.ts::date ORDER BY transactions.id ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)""")),
|
|
|
#('group', Identifier('groups','name')),
|
|
|
#('category', Identifier('categories','name')),
|
|
@@ -76,6 +86,7 @@ def get_historic_prices_statement(sort, product, unit, organic=None, limit='90 d
|
|
|
#('price_max', SQL(f"""max(price/quantity) OVER {partition}""")),
|
|
|
])
|
|
|
statement = SQL('\n').join([
|
|
|
+ _with,
|
|
|
SQL('').join([
|
|
|
SQL("SELECT"
|
|
|
"\n "),
|
|
@@ -89,6 +100,11 @@ def get_historic_prices_statement(sort, product, unit, organic=None, limit='90 d
|
|
|
"\n "),
|
|
|
SQL("\n JOIN ").join([
|
|
|
SQL("transactions"),
|
|
|
+ SQL("{table} ON ({table}.{key} = {index})").format(
|
|
|
+ table=Identifier('conv'),
|
|
|
+ key=Identifier('_from'),
|
|
|
+ index=Identifier('unit_id'),
|
|
|
+ ),
|
|
|
SQL("{table} ON {table}.{key} = {index}").format(
|
|
|
table=Identifier('units'),
|
|
|
key=Identifier('id'),
|
|
@@ -114,6 +130,7 @@ def get_historic_prices_statement(sort, product, unit, organic=None, limit='90 d
|
|
|
key=Identifier('id'),
|
|
|
index=Identifier('group_id')
|
|
|
),
|
|
|
+
|
|
|
]),
|
|
|
]),
|
|
|
get_where(product, unit, organic=organic, limit=limit),
|