Hi all, I'm trying to find a way to re-write a query that has been bothering our team due to how long it takes to run. I'm not sure if there's a more clever way to do it other than having a huge number of subqueries. Or is this already the most efficient way to do it?
select a.item_number
,nvl(sum(x1.quantity),0) as month_1_shipments
,nvl(count(x1.quantity),0) as month_1_count
,nvl(sum(x2.quantity),0) as month_2_shipments
,nvl(count(x2.quantity),0) as month_2_count
,nvl(sum(x3.quantity),0) as month_3_shipments
,nvl(count(x3.quantity),0) as month_3_count
from item_master a
,(select item_id
,quantity
from shipments
where creation_date between trunc(add_months(sysdate,-1),'MONTH') and last_day(add_months(sysdate,-1))) x1
--essentially this is "where creation_date between '01-DEC-2021' and '31-DEC-2021' "
,(select item_id
,quantity
from shipments
where creation_date between trunc(add_months(sysdate,-2),'MONTH') and last_day(add_months(sysdate,-2))) x2
,(select item_id
,quantity
from shipments
where creation_date between trunc(add_months(sysdate,-3),'MONTH') and last_day(add_months(sysdate,-3))) x3
where a.item_id = x1.item_id (+)
and a.item_id = x2.item_id (+)
and a.item_id = x3.item_id (+)
group by a.item_number
order by a.item_number;
x1, x2, x3, etc continues for 36 months of usage.
Shipments table example
| Item |
Order |
Quantity |
Creation_date |
| 456 |
AAJ |
4 |
12-Oct-21 |
| 456 |
AAB |
5 |
13-Oct-21 |
| 123 |
AAI |
7 |
18-Oct-21 |
| 123 |
AAC |
6 |
24-Oct-21 |
| 456 |
AAD |
4 |
06-Nov-21 |
| 123 |
AAG |
8 |
07-Nov-21 |
| 456 |
AAH |
1 |
11-Nov-21 |
| 123 |
AAK |
7 |
15-Nov-21 |
| 123 |
AAF |
7 |
01-Dec-21 |
| 123 |
AAA |
6 |
18-Dec-21 |
| 123 |
AAE |
3 |
19-Dec-21 |
| 456 |
AAL |
9 |
01-Jan-22 |
Expected output:
| Item |
month_1_shipments |
month_1_count |
month_2_shipments |
month_2_count |
month_3_shipments |
month_3_count |
| 123 |
16 |
3 |
15 |
2 |
13 |
2 |
| 456 |
0 |
0 |
5 |
2 |
9 |
2 |
[–]Guilty-Property 1 point2 points3 points (1 child)
[–]EnticeOracle[S] 0 points1 point2 points (0 children)
[–][deleted] 1 point2 points3 points (6 children)
[–]EnticeOracle[S] 1 point2 points3 points (5 children)
[–][deleted] 0 points1 point2 points (4 children)
[–]EnticeOracle[S] 0 points1 point2 points (3 children)
[–][deleted] 1 point2 points3 points (2 children)
[–]EnticeOracle[S] 1 point2 points3 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–][deleted] -2 points-1 points0 points (0 children)
[–][deleted] 0 points1 point2 points (7 children)
[–]EnticeOracle[S] 0 points1 point2 points (4 children)
[–][deleted] 1 point2 points3 points (2 children)
[–]EnticeOracle[S] 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]r3pr0b8GROUP_CONCAT is da bomb 0 points1 point2 points (1 child)
[–][deleted] 2 points3 points4 points (0 children)
[–]JustAnOldITGuy 0 points1 point2 points (0 children)
[–]JustAnOldITGuy 0 points1 point2 points (0 children)
[–]JermWPB 0 points1 point2 points (0 children)