|
@@ -0,0 +1,106 @@
|
|
|
+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 group_by = (CASE
|
|
|
+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
|
|
|
+THEN 'Category'
|
|
|
+ELSE 'Product'
|
|
|
+END);
|
|
|
+SET total = (SELECT sum("Quantity"::numeric) 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
|
|
|
+;
|
|
|
+SELECT "Group" AS series
|
|
|
+, sum("Quantity"::numeric) AS value
|
|
|
+FROM sqlpage_txn
|
|
|
+WHERE $group_by = 'Group' AND "Unit" = $unit_mass
|
|
|
+GROUP BY "Group"
|
|
|
+;
|
|
|
+SELECT "Category" AS series
|
|
|
+, sum("Quantity"::numeric) AS value
|
|
|
+FROM sqlpage_txn
|
|
|
+WHERE $group_by = 'Category' AND "Unit" = $unit_mass
|
|
|
+GROUP BY "Category"
|
|
|
+;
|
|
|
+SELECT "Product" AS series
|
|
|
+, sum("Quantity"::numeric) AS value
|
|
|
+FROM sqlpage_txn
|
|
|
+WHERE $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
|
|
|
+THEN 'Group'
|
|
|
+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);
|
|
|
+SELECT 'chart' AS component
|
|
|
+, 'Share by Volume - Total: '||$total||' '||$unit_volume AS title
|
|
|
+, $height AS height
|
|
|
+, $type AS type
|
|
|
+, TRUE AS labels
|
|
|
+;
|
|
|
+SELECT "Group" AS series
|
|
|
+, sum("Quantity"::numeric) AS value
|
|
|
+FROM sqlpage_txn
|
|
|
+WHERE $group_by = 'Group' AND "Unit" = $unit_volume
|
|
|
+GROUP BY "Group"
|
|
|
+;
|
|
|
+SELECT "Category" AS series
|
|
|
+, sum("Quantity"::numeric) AS value
|
|
|
+FROM sqlpage_txn
|
|
|
+WHERE $group_by = 'Category' AND "Unit" = $unit_volume
|
|
|
+GROUP BY "Category"
|
|
|
+;
|
|
|
+SELECT "Product" AS series
|
|
|
+, sum("Quantity"::numeric) AS value
|
|
|
+FROM sqlpage_txn
|
|
|
+WHERE $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
|
|
|
+THEN 'Group'
|
|
|
+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);
|
|
|
+SELECT 'chart' AS component
|
|
|
+, 'Share by Volume - Total: '||$total||' '||$unit_count_chart AS title
|
|
|
+, $height AS height
|
|
|
+, $type AS type
|
|
|
+, TRUE AS labels
|
|
|
+;
|
|
|
+SELECT "Group" AS series
|
|
|
+, sum("Quantity"::numeric) AS value
|
|
|
+FROM sqlpage_txn
|
|
|
+WHERE $group_by = 'Group' AND "Unit" = $unit_count_chart
|
|
|
+GROUP BY "Group"
|
|
|
+;
|
|
|
+SELECT "Category" AS series
|
|
|
+, sum("Quantity"::numeric) AS value
|
|
|
+FROM sqlpage_txn
|
|
|
+WHERE $group_by = 'Category' AND "Unit" = $unit_count_chart
|
|
|
+GROUP BY "Category"
|
|
|
+;
|
|
|
+SELECT "Product" AS series
|
|
|
+, sum("Quantity"::numeric) AS value
|
|
|
+FROM sqlpage_txn
|
|
|
+WHERE $group_by = 'Product' AND "Unit" = $unit_count_chart
|
|
|
+GROUP BY "Product"
|
|
|
+;
|