Created
August 6, 2018 12:02
-
-
Save eugene-eeo/44298d1beb48aeb2720494c8b5b33035 to your computer and use it in GitHub Desktop.
sqls
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT | |
r.id as r_id, | |
i.id as i_id, | |
SUM(u.quantity) as per_resident_total | |
FROM | |
resident as r, | |
inventory_item as i, | |
inventory_usage as u | |
WHERE | |
resident.id = inventory_usage.id AND | |
inventory_item.id = inventory_usage.inventory_item_id AND | |
inventory_item_usage.deleted IS NULL | |
GROUP BY | |
r.id, | |
i.id; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT | |
t1.*, | |
t2.per_item_total | |
FROM ( | |
SELECT | |
r.id as r_id, | |
i.id as i_id, | |
SUM(u.quantity) as per_resident_total | |
FROM | |
resident as r, | |
inventory_item as i, | |
inventory_usage as u | |
WHERE | |
resident.id = inventory_usage.id AND | |
inventory_item.id = inventory_usage.inventory_item_id AND | |
inventory_item_usage.deleted IS NULL | |
GROUP BY | |
r.id, | |
i.id | |
) AS t1 | |
JOIN ( | |
SELECT | |
i.id as i_id, | |
SUM(u.quantity) as per_item_total | |
FROM | |
inventory_item as i, | |
inventory_usage as u | |
WHERE | |
inventory_item.id = inventory_usage.inventory_item_id AND | |
inventory_item_usage.deleted IS NULL | |
GROUP BY | |
i.id | |
) AS t2 | |
ON t1.i_id = t2.i_id; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment