I have a query where I have been using a user input (i.e. &&year) to enter years to be run in the query:
SELECT
t.type_code ,
t.sub_type_code,
COUNT(t.unique_number_id),
SUM(t.area_in_hectares)
FROM
mytable t
WHERE
to_char(t.start_date, 'YYYY') <= '&&year' AND
(
to_char(t.event_date, 'YYYY') > '&&year' OR
t.event_date IS NULL OR
t.type_code = 'P'
)
GROUP BY
t.type_code ,
t.sub_type_code
I wanted to make this more efficient and given some of my other programming experience I had thought that, if I can loop over a range of years, I can make this query much more efficient to run. I am wondering if there is a way to iteratively inject the year from a list into the user variables instead of manually typing them in. I run the query annually and user run back almost 100 years and clearly it is not efficient to enter years manually.
I've been reading about for loops and while loops but I ma not clear if I can use those in Oracle SQL.
Thanks for any thoughts or suggestions.