I'm using a PaaS (Asset Works) to create a report that needs looping. I have a bus table and need to create a spreadsheet that lists the bus replacements. Some buses need to be replaced every 10 years, others every 12 years. My report needs to list all the replacements over the next 50 years.
I've found the looping structure for Counter in 1..MaxCounter loop and see how to execute a query and put the values INTO another table. The problem is this isn't my Oracle db. I can't create a table, temp or perm. So my query needs to hold the values for use in my report.
Any suggestions?
Thanks in advance! Bruce
Declare
Counter number;
MaxCounter number := 50;
StartYear number := 2021;
BEGIN
for Counter in 1..MaxCounter loop
Select * From eq_main where year(Delivery_Date) = startyear + maxcounter;
-- Save this rowset somewhere...
end Loop;
commit;
end;
If I got it right you need the data for 50 years in advance about bus replacements per year and bus group - according to validity periods in years per bus group.
Some dummy sample data to work with:
Create 2 CTEs - one for list of next 50 years and another ( joining your data to the first ) with the years when any particular group period requires the replacement :
Now we can group, count and calculate totals of the data from replacements cte to get how many busses per group should be replaced:
Option 1.
If you want all years to be listed, even if there are no replacements, then just change Inner Join in replacements CTE with Left Join.
Option 2. - PIVOT