I need to write an oracle query to find total contract quantities ordered each week (Monday to Sunday) between 2 given dates (say, :startdate = '28-Nov-23' and :enddate = '15-Dec-23') and display the Date values as column names.
TABLE A:
CONTRACTNO QTY SUPPLIER CDATE
---------- --- -------- -----
23231 12 LG 28-Nov-23
23232 2 SM 23-Nov-23
23233 44 AP 11-Dec-23
23234 9 BR 25-Dec-23
23235 22 SM 15-Dec-23
23236 7 AP 08-Dec-23
23237 1 LG 30-Nov-23
Expected Output:
Wk_27-Nov-23 Wk_04-Dec-23 Wk_11-Dec-23
----------- ----------- -----------
13 7 66
where Wk_27-Nov-23 = Sum(Qty) from 27-Nov-23 to 03-Dec-23, next week from 04-Dec-23 to 10-Dec-23 and so on till the Monday before :enddate.
I have no idea where to start as I'm fairly new to Oracle SQL. So far, I have inserted Mondays between start and end dates into a Temp table using the below query but need to know how to apply this to SUM()/GROUP.
select MonDate from (select (sysdate-7 + rownum -1) MonDate
from all_objects
where rownum <= (to_date('15-DEC-23', 'dd-mon-yy')) - (sysdate-7))
where rtrim(to_char(MonDate, 'DAY')) = 'MONDAY';
Thanks!
startandenddates, but it's not clear what's thesysdate-7part is doing. If you want to generate a list of Monday dates between two specific dates, you can use the CONNECT BY, which is a powerful tool for generating sequences of rows.