|
@@ -62,6 +62,13 @@ def get_where(unit, product=None, category=None, group=None, organic=None, limit
|
|
|
def get_historic_prices_statement(unit, sort=None, product=None, category=None, group=None, 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 ''}"
|
|
|
+ sort_sql = SQL('').join([
|
|
|
+ SQL('{sort} {direction},').format(
|
|
|
+ sort=Identifier(f'{sort}'),
|
|
|
+ direction = SQL('DESC' if sort == 'ts' else 'ASC')
|
|
|
+ ),
|
|
|
+ ]) if sort is not None else SQL('')
|
|
|
+
|
|
|
_with = SQL("""WITH conv AS (
|
|
|
SELECT
|
|
|
_1._from, _1._to, factor FROM conversions AS _1
|
|
@@ -137,8 +144,7 @@ def get_historic_prices_statement(unit, sort=None, product=None, category=None,
|
|
|
]),
|
|
|
get_where(unit, product=product, category=category, group=group, organic=organic, limit=limit),
|
|
|
SQL('ORDER BY {organic_sort} {sort} code, product, category, "group", "$/unit" ASC, ts DESC').format(
|
|
|
- sort=SQL(f'{Identifier(sort)} {direction},' if sort is not None else ''),
|
|
|
- direction=SQL('DESC' if sort == 'ts' else 'ASC'),
|
|
|
+ sort=sort_sql,
|
|
|
organic_sort=SQL(organic_sort),
|
|
|
),
|
|
|
])
|