|
@@ -1,106 +1,119 @@
|
|
|
SET height = '500';
|
|
|
SET type = 'pie';
|
|
|
+
|
|
|
SET unit_count_chart = (SELECT v#>>'{}' FROM json_array_elements($unit_count::json) j(v) LIMIT 1);
|
|
|
-SET display_by_count = (SELECT count(*) FROM json_array_elements($unit_count::json) j(v)) == 1;
|
|
|
+SET display_by_count = (SELECT count(*) FROM json_array_elements($unit_count::json) j(v)) == 1
|
|
|
+AND (EXISTS (SELECT 1 FROM sqlpage_txn WHERE unit = $unit_count_chart LIMIT 1));
|
|
|
+SET display_by_mass = (EXISTS (SELECT 1 FROM sqlpage_txn WHERE unit = $unit_mass LIMIT 1));
|
|
|
+SET display_by_volume = (EXISTS (SELECT 1 FROM sqlpage_txn WHERE unit = $unit_volume LIMIT 1));
|
|
|
|
|
|
SET group_by = (CASE
|
|
|
-WHEN (SELECT count(DISTINCT "Group") FROM sqlpage_txn WHERE "Unit" = $unit_mass) > 2
|
|
|
+WHEN (SELECT count(DISTINCT "group") FROM sqlpage_txn WHERE unit = $unit_mass) > 2
|
|
|
THEN 'Group'
|
|
|
-WHEN (SELECT count(DISTINCT "Category") FROM sqlpage_txn WHERE "Unit" = $unit_mass) > 2
|
|
|
+WHEN (SELECT count(DISTINCT category) FROM sqlpage_txn WHERE unit = $unit_mass) > 2
|
|
|
THEN 'Category'
|
|
|
ELSE 'Product'
|
|
|
END);
|
|
|
-SET total = (SELECT sum("Quantity"::numeric) FROM sqlpage_txn
|
|
|
-WHERE "Unit" = $unit_mass);
|
|
|
+SET total = (SELECT sum(quantity) FROM sqlpage_txn WHERE unit = $unit_mass);
|
|
|
SELECT 'chart' AS component
|
|
|
, 'Share by Weight - Total: '||$total||' '||$unit_mass AS title
|
|
|
, $height AS height
|
|
|
, $type AS type
|
|
|
, TRUE AS labels
|
|
|
+WHERE $display_by_mass::bool
|
|
|
;
|
|
|
-SELECT "Group" AS series
|
|
|
-, sum("Quantity"::numeric) AS value
|
|
|
+SELECT "group" AS series
|
|
|
+, sum(quantity) AS value
|
|
|
FROM sqlpage_txn
|
|
|
-WHERE $group_by = 'Group' AND "Unit" = $unit_mass
|
|
|
-GROUP BY "Group"
|
|
|
+WHERE $display_by_mass::bool
|
|
|
+AND $group_by = 'Group' AND unit = $unit_mass
|
|
|
+GROUP BY "group"
|
|
|
;
|
|
|
-SELECT "Category" AS series
|
|
|
-, sum("Quantity"::numeric) AS value
|
|
|
+SELECT category AS series
|
|
|
+, sum(quantity) AS value
|
|
|
FROM sqlpage_txn
|
|
|
-WHERE $group_by = 'Category' AND "Unit" = $unit_mass
|
|
|
-GROUP BY "Category"
|
|
|
+WHERE $display_by_mass::bool
|
|
|
+AND $group_by = 'Category' AND unit = $unit_mass
|
|
|
+GROUP BY category
|
|
|
;
|
|
|
-SELECT "Product" AS series
|
|
|
-, sum("Quantity"::numeric) AS value
|
|
|
+SELECT product AS series
|
|
|
+, sum(quantity) AS value
|
|
|
FROM sqlpage_txn
|
|
|
-WHERE $group_by = 'Product' AND "Unit" = $unit_mass
|
|
|
-GROUP BY "Product"
|
|
|
+WHERE $display_by_mass::bool
|
|
|
+AND $group_by = 'Product' AND unit = $unit_mass
|
|
|
+GROUP BY product
|
|
|
;
|
|
|
|
|
|
SET group_by = (CASE
|
|
|
-WHEN (SELECT count(DISTINCT "Group") FROM sqlpage_txn WHERE "Unit" = $unit_volume) > 2
|
|
|
+WHEN (SELECT count(DISTINCT "group") FROM sqlpage_txn WHERE unit = $unit_volume) > 2
|
|
|
THEN 'Group'
|
|
|
-WHEN (SELECT count(DISTINCT "Category") FROM sqlpage_txn WHERE "Unit" = $unit_volume) > 2
|
|
|
+WHEN (SELECT count(DISTINCT category) FROM sqlpage_txn WHERE unit = $unit_volume) > 2
|
|
|
THEN 'Category'
|
|
|
ELSE 'Product'
|
|
|
END);
|
|
|
-SET total = (SELECT sum("Quantity"::numeric) FROM sqlpage_txn
|
|
|
-WHERE "Unit" = $unit_volume);
|
|
|
+SET total = (SELECT sum(quantity) FROM sqlpage_txn WHERE unit = $unit_volume);
|
|
|
SELECT 'chart' AS component
|
|
|
, 'Share by Volume - Total: '||$total||' '||$unit_volume AS title
|
|
|
, $height AS height
|
|
|
, $type AS type
|
|
|
, TRUE AS labels
|
|
|
+WHERE $display_by_volume::bool
|
|
|
;
|
|
|
-SELECT "Group" AS series
|
|
|
-, sum("Quantity"::numeric) AS value
|
|
|
+SELECT "group" AS series
|
|
|
+, sum(quantity) AS value
|
|
|
FROM sqlpage_txn
|
|
|
-WHERE $group_by = 'Group' AND "Unit" = $unit_volume
|
|
|
-GROUP BY "Group"
|
|
|
+WHERE $display_by_volume::bool
|
|
|
+AND $group_by = 'Group' AND unit = $unit_volume
|
|
|
+GROUP BY "group"
|
|
|
;
|
|
|
-SELECT "Category" AS series
|
|
|
-, sum("Quantity"::numeric) AS value
|
|
|
+SELECT category AS series
|
|
|
+, sum(quantity) AS value
|
|
|
FROM sqlpage_txn
|
|
|
-WHERE $group_by = 'Category' AND "Unit" = $unit_volume
|
|
|
-GROUP BY "Category"
|
|
|
+WHERE $display_by_volume::bool
|
|
|
+AND $group_by = 'Category' AND unit = $unit_volume
|
|
|
+GROUP BY category
|
|
|
;
|
|
|
-SELECT "Product" AS series
|
|
|
-, sum("Quantity"::numeric) AS value
|
|
|
+SELECT product AS series
|
|
|
+, sum(quantity) AS value
|
|
|
FROM sqlpage_txn
|
|
|
-WHERE $group_by = 'Product' AND "Unit" = $unit_volume
|
|
|
-GROUP BY "Product"
|
|
|
+WHERE $display_by_volume::bool
|
|
|
+AND $group_by = 'Product' AND unit = $unit_volume
|
|
|
+GROUP BY product
|
|
|
;
|
|
|
|
|
|
SET group_by = (CASE
|
|
|
-WHEN (SELECT count(DISTINCT "Group") FROM sqlpage_txn WHERE "Unit" = $unit_count_chart) > 2
|
|
|
+WHEN (SELECT count(DISTINCT "group") FROM sqlpage_txn WHERE unit = $unit_count_chart) > 2
|
|
|
THEN 'Group'
|
|
|
-WHEN (SELECT count(DISTINCT "Category") FROM sqlpage_txn WHERE "Unit" = $unit_count_chart) > 2
|
|
|
+WHEN (SELECT count(DISTINCT category) FROM sqlpage_txn WHERE unit = $unit_count_chart) > 2
|
|
|
THEN 'Category'
|
|
|
ELSE 'Product'
|
|
|
END);
|
|
|
-SET total = (SELECT sum("Quantity"::numeric) FROM sqlpage_txn
|
|
|
-WHERE "Unit" = $unit_count_chart);
|
|
|
+SET total = (SELECT sum(quantity) FROM sqlpage_txn WHERE unit = $unit_count_chart);
|
|
|
SELECT 'chart' AS component
|
|
|
, 'Share by Volume - Total: '||$total||' '||$unit_count_chart AS title
|
|
|
, $height AS height
|
|
|
, $type AS type
|
|
|
, TRUE AS labels
|
|
|
+WHERE $display_by_count::bool
|
|
|
;
|
|
|
-SELECT "Group" AS series
|
|
|
-, sum("Quantity"::numeric) AS value
|
|
|
+SELECT "group" AS series
|
|
|
+, sum(quantity) AS value
|
|
|
FROM sqlpage_txn
|
|
|
-WHERE $group_by = 'Group' AND "Unit" = $unit_count_chart
|
|
|
-GROUP BY "Group"
|
|
|
+WHERE $display_by_count::bool
|
|
|
+AND $group_by = 'Group' AND unit = $unit_count_chart
|
|
|
+GROUP BY "group"
|
|
|
;
|
|
|
-SELECT "Category" AS series
|
|
|
-, sum("Quantity"::numeric) AS value
|
|
|
+SELECT category AS series
|
|
|
+, sum(quantity) AS value
|
|
|
FROM sqlpage_txn
|
|
|
-WHERE $group_by = 'Category' AND "Unit" = $unit_count_chart
|
|
|
-GROUP BY "Category"
|
|
|
+WHERE $display_by_count::bool
|
|
|
+AND $group_by = 'Category' AND unit = $unit_count_chart
|
|
|
+GROUP BY category
|
|
|
;
|
|
|
-SELECT "Product" AS series
|
|
|
-, sum("Quantity"::numeric) AS value
|
|
|
+SELECT product AS series
|
|
|
+, sum(quantity) AS value
|
|
|
FROM sqlpage_txn
|
|
|
-WHERE $group_by = 'Product' AND "Unit" = $unit_count_chart
|
|
|
-GROUP BY "Product"
|
|
|
+WHERE $display_by_count::bool
|
|
|
+AND $group_by = 'Product' AND unit = $unit_count_chart
|
|
|
+GROUP BY product
|
|
|
;
|