Преглед изворни кода

remove redundant SELECT for function to make query more consise

Daniel Sheffield пре 2 година
родитељ
комит
2c8541b984
1 измењених фајлова са 21 додато и 42 уклоњено
  1. 21 42
      app/price_view.py

+ 21 - 42
app/price_view.py

@@ -49,11 +49,8 @@ def get_where(unit, product=None, category=None, group=None, organic=None, limit
             )
         )
     where.append(SQL(
-        '(SELECT convert_unit(units.name, {unit}, {product}) IS NOT NULL)'
-    ).format(
-        unit=Literal(unit),
-        product=Literal(product)
-    ))
+        'convert_unit(units.name, {unit}, {product}) IS NOT NULL'
+    ).format(unit=Literal(unit), product=Literal(product)))
     return SQL('').join([
         SQL("WHERE"
             "\n      "),
@@ -75,43 +72,25 @@ def get_historic_prices_statement(unit, sort=None, product=None, category=None,
         ('ts_raw', SQL("""(transactions.ts AT TIME ZONE 'UTC')::timestamp without time zone""")),
         ('%d/%m/%y %_I%P', SQL("""(transactions.ts AT TIME ZONE 'UTC')::timestamp without time zone""")),
         ('code', Identifier('stores', 'code')),
-        ('$/unit', SQL(
-            f"""TRUNC(price / (quantity * (
-  SELECT convert_unit(units.name,{{unit}}, {{product}}))), 4)"""
-        ).format(
-            unit=Literal(unit),
-            product=Literal(product)
-        )),
-        ('avg', SQL(
-            f"""TRUNC(sum(price) OVER {partition} / sum(quantity * (
-  SELECT convert_unit(units.name, {{unit}}, {{product}}))) OVER {partition}, 4
-)"""
-        ).format(
-            unit=Literal(unit),
-            product=Literal(product)
-        )),
-        ('min', SQL(
-            f"""TRUNC(min(price / (quantity * (
-  SELECT convert_unit(units.name, {{unit}}, {{product}})))) OVER {partition}, 4)"""
-        ).format(
-            unit=Literal(unit),
-            product=Literal(product)
-        )),
-        ('max', SQL(
-            f"""TRUNC(max(price / (quantity * (
-  SELECT convert_unit(units.name, {{unit}}, {{product}})))) OVER {partition}, 4)"""
-        ).format(
-            unit=Literal(unit),
-            product=Literal(product)
-        )),
-        ('price', SQL(f"""TRUNC(price, 4)::numeric""")),
-        ('quantity', SQL(
-            f"""TRUNC(quantity*(
-  SELECT convert_unit(units.name, {{unit}}, {{product}})), 4)::numeric
-""").format(
-    unit=Literal(unit),
-    product=Literal(product)
-        )),
+        ('$/unit', SQL("""TRUNC(
+    price / quantity / convert_unit(units.name, {unit}, {product}), 4
+)""").format(unit=Literal(unit), product=Literal(product))),
+        ('avg', SQL(f"""TRUNC(sum(price) OVER {partition} / sum(
+    quantity * convert_unit(units.name, {{unit}}, {{product}})
+) OVER {partition}, 4)
+""").format(unit=Literal(unit), product=Literal(product))),
+        ('min', SQL(f"""TRUNC(min(
+    price / quantity / convert_unit(units.name, {{unit}}, {{product}})
+) OVER {partition}, 4)
+""").format(unit=Literal(unit), product=Literal(product))),
+        ('max', SQL(f"""TRUNC(max(
+    price / quantity / convert_unit(units.name, {{unit}}, {{product}})
+) OVER {partition}, 4)
+""").format(unit=Literal(unit), product=Literal(product))),
+        ('price', SQL("""TRUNC(price, 4)""")),
+        ('quantity', SQL("""TRUNC(
+    quantity * convert_unit(units.name, {unit}, {product}), 4
+)""").format(unit=Literal(unit), product=Literal(product))),
         ('product', Identifier('products','name')),
         ('category', Identifier('categories', 'name')),
         ('group', Identifier('groups', 'name')),