Skip to content

Commit b48d415

Browse files
authored
Merge pull request #688 from boxwise/update-annual-report-query
update annual report query
2 parents 5768812 + 709ab8f commit b48d415

1 file changed

Lines changed: 82 additions & 3 deletions

File tree

db/queries/annual_report.sql

Lines changed: 82 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,11 @@ BoxHistory AS (
135135
h.id AS id
136136
FROM history h
137137
JOIN ValidBoxes s ON h.record_id = s.id
138-
AND ((h.to_int IS NOT null AND h.id >= 1324559) OR h.changes = "Record created")
138+
AND ((h.to_int IS NOT null AND h.id >= 1324559)
139+
OR h.changes = "Record created"
140+
OR h.changes = "Record deleted"
141+
OR h.changes = "Box was undeleted."
142+
)
139143
AND h.tablename = 'stock'
140144
ORDER BY record_id, changedate DESC, id DESC
141145
),
@@ -262,6 +266,30 @@ BoxStateChangeVersions AS (
262266
FROM HistoryReconstruction h
263267
WHERE h.changes = 'box_state_id'
264268
),
269+
DeletedBoxes AS (
270+
SELECT
271+
h.box_id,
272+
h.box_state_id,
273+
date(h.changedate) as moved_on,
274+
h.items AS number_of_items,
275+
h.location_id,
276+
h.product_id AS product,
277+
h.size_id
278+
FROM HistoryReconstruction h
279+
WHERE h.changes = 'Record deleted'
280+
),
281+
UndeletedBoxes AS (
282+
SELECT
283+
h.box_id,
284+
h.box_state_id,
285+
date(h.changedate) as moved_on,
286+
h.items AS number_of_items,
287+
h.location_id,
288+
h.product_id AS product,
289+
h.size_id
290+
FROM HistoryReconstruction h
291+
WHERE h.changes = 'Box was undeleted.'
292+
),
265293
CreatedDonatedBoxes AS (
266294
SELECT
267295
h.box_id,
@@ -277,8 +305,58 @@ CreatedDonatedBoxes AS (
277305

278306
-- MAIN QUERY
279307

280-
-- Collect information about boxes created in donated state
308+
-- Collect information about deleted/undeleted boxes. Stats are labeled as "Deleted",
309+
-- with deleted boxes/items containing positive, and undeleted ones counting negative.
310+
select
311+
t.moved_on,
312+
p.category_id,
313+
TRIM(LOWER(p.name)) AS product_name,
314+
p.gender_id AS gender,
315+
t.size_id,
316+
GROUP_CONCAT(DISTINCT tr.tag_id) AS tag_ids,
317+
"Deleted" AS target_id,
318+
"BoxState" AS target_type,
319+
c.id as base_id,
320+
c.name as base_name,
321+
o.label as org_name,
322+
count(t.box_id) AS boxes_count,
323+
sum(t.number_of_items) AS items_count
324+
FROM DeletedBoxes t
325+
JOIN products p ON p.id = t.product
326+
JOIN locations loc ON loc.id = t.location_id
327+
JOIN camps c ON c.id = loc.camp_id
328+
JOIN organisations o ON o.id = c.organisation_id
329+
LEFT OUTER JOIN tags_relations tr ON tr.object_id = t.box_id AND tr.object_type = "Stock" AND tr.deleted_on IS NULL
330+
GROUP BY moved_on, p.category_id, p.name, p.gender_id, t.size_id, loc.label, c.id, c.name, o.label
331+
332+
UNION ALL
333+
281334
select
335+
t.moved_on,
336+
p.category_id,
337+
TRIM(LOWER(p.name)) AS product_name,
338+
p.gender_id AS gender,
339+
t.size_id,
340+
GROUP_CONCAT(DISTINCT tr.tag_id) AS tag_ids,
341+
"Deleted" AS target_id,
342+
"BoxState" AS target_type,
343+
c.id as base_id,
344+
c.name as base_name,
345+
o.label as org_name,
346+
-count(t.box_id) AS boxes_count,
347+
-sum(t.number_of_items) AS items_count
348+
FROM UndeletedBoxes t
349+
JOIN products p ON p.id = t.product
350+
JOIN locations loc ON loc.id = t.location_id
351+
JOIN camps c ON c.id = loc.camp_id
352+
JOIN organisations o ON o.id = c.organisation_id
353+
LEFT OUTER JOIN tags_relations tr ON tr.object_id = t.box_id AND tr.object_type = "Stock" AND tr.deleted_on IS NULL
354+
GROUP BY moved_on, p.category_id, p.name, p.gender_id, t.size_id, loc.label, c.id, c.name, o.label
355+
356+
UNION ALL
357+
358+
-- Collect information about boxes created in donated state
359+
SELECT
282360
t.moved_on,
283361
p.category_id,
284362
TRIM(LOWER(p.name)) AS product_name,
@@ -303,7 +381,7 @@ GROUP BY moved_on, p.category_id, p.name, p.gender_id, t.size_id, loc.label, c.i
303381
UNION ALL
304382

305383
-- Collect information about boxes being moved between states InStock and Donated
306-
select
384+
SELECT
307385
t.moved_on,
308386
p.category_id,
309387
TRIM(LOWER(p.name)) AS product_name,
@@ -407,3 +485,4 @@ JOIN camps c ON c.id = loc.camp_id
407485
JOIN organisations o ON o.id = c.organisation_id
408486
LEFT OUTER JOIN tags_relations tr ON tr.object_id = b.id AND tr.object_type = "Stock" AND tr.deleted_on IS NULL
409487
GROUP BY moved_on, p.category_id, p.name, p.gender_id, b.size_id, bs.label, c.id, c.name, o.label
488+
;

0 commit comments

Comments
 (0)