I know how to do this with MS-Access and was wondering how to write it in Oracle. We were given a script, a portion of which I include below, which is run each month. We need to expand it to run for multiple years rather than a single year. Since the years and dates need to go together, I was thinking that a loop would be the way to go, although certainly open to alternative ideas.
The output of the script of Unioned SQL statements produces a CSV file. When the script was originally created, we only had one data set (year 2008) eventually we will have 3 year groupings of data. (2008, 2009, 2010; 2009, 2010, 2011; etc.) For Jan 2009, we would run it for Year = 2009 and Cutoff Date 01-Jan-2009 (may not always be the first of the month as we could be requested to run for different dates within the month and when running for different years), then the same query needs to be run for 2008 with a cutoff date of 01-Jan-2008, or the date the requestor specifies.
As an example we may decide to run the data in 2009 as of 05-Jan-2009 and for 2008 we may use 07-Jan-2008; The first Monday in each year.
There are 14 output csv files created and each file is made up of 7 Union queries.
How would we set up the loop and pass the dates and years. Another aspect that we would like to do with the loop, unless there is another approach, is that the person who wrote the script repeated all the scripts because we wanted to have the csv files saved in two ways, the first is a generic name so that our access/excel application can directly read the file, the other gives the csv files a dated file name so that we have an historical record of what was run at a particular day. Currently we pass the variables for the Cutoff Date and the historical file date by entering it into a command line of a DOS bat file that then runs the code to SQL*Plus.
Here is a partial code
The output of the script of Unioned SQL statements produces a CSV file. When the script was originally created, we only had one data set (year 2008) eventually we will have 3 year groupings of data. (2008, 2009, 2010; 2009, 2010, 2011; etc.) For Jan 2009, we would run it for Year = 2009 and Cutoff Date 01-Jan-2009 (may not always be the first of the month as we could be requested to run for different dates within the month and when running for different years), then the same query needs to be run for 2008 with a cutoff date of 01-Jan-2008, or the date the requestor specifies.
As an example we may decide to run the data in 2009 as of 05-Jan-2009 and for 2008 we may use 07-Jan-2008; The first Monday in each year.
There are 14 output csv files created and each file is made up of 7 Union queries.
How would we set up the loop and pass the dates and years. Another aspect that we would like to do with the loop, unless there is another approach, is that the person who wrote the script repeated all the scripts because we wanted to have the csv files saved in two ways, the first is a generic name so that our access/excel application can directly read the file, the other gives the csv files a dated file name so that we have an historical record of what was run at a particular day. Currently we pass the variables for the Cutoff Date and the historical file date by entering it into a command line of a DOS bat file that then runs the code to SQL*Plus.
Here is a partial code
Code:
set feedback off
set pause off
set pagesize 50000
set newpage 0
set heading off
set verify off
set termout off
spool "c:\temp\first_gen.csv"
select 'YEAR'||chr(9)||'STU_POP'||chr(9)||'QNAME'||chr(9)||
'SORT_STATUS'||chr(9)||'STATUS'||chr(9)||'TOTAL'||chr(9)||
'NFG'||chr(9)||'FG'||chr(9)||'DECLINED_TO_ANSWER'||chr(9)||'UNKNOWN' as stuff
from dual
UNION ALL
select year||chr(9)||stu_pop||chr(9)||'FIRST_GEN'||chr(9)||
'1.Applicants'||chr(9)||
'Applicants'||chr(9)||
sum(applied)||chr(9)||
sum(case when first_gen = 'N' and applied = 1 then 1 else 0 end)||chr(9)||
sum(case when first_gen = 'Y' and applied = 1 then 1 else 0 end)||chr(9)||
sum(case when first_gen = 'D' and applied = 1 then 1 else 0 end)||chr(9)||
sum(case when first_gen is null and applied = 1 then 1 else 0 end)
from usf_stu.appl_stats
where stu_population in ('F', 'T')
and student_level = 'UG'
and appl_date < [red]'&cut_off'[/red]
and year = [red]2008[/red]
and college <> 'PS'
group by year, stu_pop, 'FIRST_GEN'
UNION ALL
...
...
spool off
----------------------------------------------
-- Put the run date in the output file name --
----------------------------------------------
--spool "c:\temp\first_gen[red]_'&1'[/red].csv"
select 'YEAR'||chr(9)||'STU_POP'||chr(9)||'QNAME'||chr(9)||
'SORT_STATUS'||chr(9)||'STATUS'||chr(9)||'TOTAL'||chr(9)||
'NFG'||chr(9)||'FG'||chr(9)||'DECLINED_TO_ANSWER'||chr(9)||'UNKNOWN' as stuff
from dual
UNION ALL
select year||chr(9)||stu_pop||chr(9)||'FIRST_GEN'||chr(9)||
'1.Applicants'||chr(9)||
'Applicants'||chr(9)||
sum(applied)||chr(9)||
sum(case when first_gen = 'N' and applied = 1 then 1 else 0 end)||chr(9)||
sum(case when first_gen = 'Y' and applied = 1 then 1 else 0 end)||chr(9)||
sum(case when first_gen = 'D' and applied = 1 then 1 else 0 end)||chr(9)||
sum(case when first_gen is null and applied = 1 then 1 else 0 end)
from usf_stu.appl_stats
where stu_population in ('F', 'T')
and student_level = 'UG'
and appl_date < '&cut_off'
and year = 2008 and college <> 'PS'
group by year, stu_pop, 'FIRST_GEN'
UNION ALL
...
...