SQL Language (MOSC)

MOSC Banner

pivot query with dynamic in clause

edited Aug 8, 2020 3:05AM in SQL Language (MOSC) 8 commentsAnswered ✓

I'm working on a pivot query which tracks by time (by minute).  I need it to report on the last 12 or 24 hours or several days.  But, the problem is I can't determine how to build the "in" clause.  Here it is:

with pivot_data as (select 'prod' as src, to_char(datetime,'YYYY-MM-DD HH24:MI') as time, sum(1) as cnt
                                  from prod
                                 where datetime > sysdate - .5
                                 group by  to_char(datetime,'YYYY-MM-DD HH24:MI')
union all
select 'test' as src, to_char(datetime,'YYYY-MM-DD HH24:MI') as time, sum(1) as cnt
                                  from test
                                 where datetime > sysdate - .5
                                 group by  to_char(datetime,'YYYY-MM-DD HH24:MI'))
select *
from pivot_data
pivot (sum(cnt)
for time in ('2015-08-27 23:30','2015-08-27 23:31','2015-08-27 23:32','2015-08-27 23:33', . . . )

Tagged:

Howdy, Stranger!

Log In

To view full details, sign in to My Oracle Support Community.

Register

Don't have a My Oracle Support Community account? Click here to get started.

Category Leaderboard

Top contributors this month

New to My Oracle Support Community? Visit our Welcome Center

MOSC Help Center