views:

101

answers:

1

I want to use a list of arbitrary numbers as a sort of input to a select. Option A, of course, is to create a temporary table that contains just the values (e.g., 1,2,3).

I hope that you folks know what Option >A is.

Suppose the statement is like:

select Fx,
XXXXXX as Foo
from MyTable
where MyTest depends on each XXXXXX

So if I could magically make XXXXXX a list of values (1,2,3), I'd have a resultset like:

    My val | Foo
    -------+---
       cat | 1
     mouse | 2
cheesecake | 3

Again, I could source the inputs from a table, but I prefer not to if it's not necessary. Gurus, please chime in.

TIA.

A: 

You will probably find success using the ROW_NUMBER() Window Aggregate function.

Random Order

SELECT CALENDAR_DATE
     , ROW_NUMBER()
       OVER (ORDER BY 1)
FROM SYS_CALENDAR.CALENDAR
WHERE CALENDAR_DATE BETWEEN DATE '2010-06-01' AND DATE 
;

OR Order by the column

SELECT CALENDAR_DATE
     , ROW_NUMBER()
       OVER (ORDER BY CALENDAR_DATE)
FROM SYS_CALENDAR.CALENDAR
WHERE CALENDAR_DATE BETWEEN DATE '2010-06-01' AND DATE 
;

OR Partition by another column to restart the sequence

SELECT CALENDAR_DATE
     , YEAR_OF_CALENDAR
     , ROW_NUMBER()
       OVER (PARTITION BY YEAR_OF_CALENDAR
             ORDER BY CALENDAR_DATE)
FROM SYS_CALENDAR.CALENDAR
WHERE CALENDAR_DATE BETWEEN DATE '2009-11-01' AND DATE 
;

;

RobPaller