Skip to content
Advertisement

How to dynamically select columns in Oracle? is this possible?

I have the below data set:

Currently i have the below query:

Is there a way to alter te above query so that it asks for the desired year, uses that as an input parameter and only grabs data for that partciular year + the 5 years prior?

So for example: If i inputed 2017 , it would return the following result:

Is there any way to do this? (similar to a parameter in a function / stored procedure)? I was thinking of substituting the year #s with some sort of parameter and inputting that …

Thanks in advance.

Advertisement

Answer

You can UNPIVOT your columns into rows, so that you can apply aggregates like MAX on them.

Like so,

Once you have the data unpivoted like that, you can use aggregate functions and GROUP BY to get the results you’re looking for:

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement