sql >> Database teknologi >  >> RDS >> Oracle

Oracle SQL - Sådan får du tællinger baseret på datoer i flere kolonner i ORACLE

Her er en måde at gøre dette ved at bruge PIVOT-operatoren.

with 
     inputs (REGID, SESSION_START_DATETIME, USAGEID) as (  
       select 1 , to_date('7/11/2016', 'mm/dd/yyyy'), 1 from dual union all 
       select 1 , to_date('6/10/2016', 'mm/dd/yyyy'), 1 from dual union all       
       select 1 , to_date('6/09/2016', 'mm/dd/yyyy'), 1 from dual union all 
       select 1 , to_date('5/04/2016', 'mm/dd/yyyy'), 1 from dual union all 
       select 1 , to_date('5/04/2016', 'mm/dd/yyyy'), 1 from dual union all 
       select 1 , to_date('5/04/2016', 'mm/dd/yyyy'), 1 from dual
     )
select * from (
  select regid, session_start_datetime,
         case when trunc(sysdate) - session_start_datetime between  0 and 30
                   then  '0-30_days_usagecount'
              when trunc(sysdate) - session_start_datetime between 31 and 60
                   then '31-60_days_usagecount'
              when trunc(sysdate) - session_start_datetime between 61 and 90
                   then '61-90_days_usagecount'
              end
         as col
  from inputs
)
pivot (  count(session_start_datetime)
            for col in ( '0-30_days_usagecount', '31-60_days_usagecount',
                        '61-90_days_usagecount'
                       )
      )
;



     REGID '0-30_days_usagecount' '31-60_days_usagecount' '61-90_days_usagecount'
---------- ---------------------- ----------------------- -----------------------
         1                      1                       2                       3

1 row selected.


  1. Vælg kolonner på tværs af forskellige databaser

  2. Oracle-adgang fra iOS

  3. Parallelle mysql-forespørgsler i php?

  4. Læs POST-data fra Oracle Forms App