sql >> Database >  >> RDS >> Oracle

Oracle SQL - Hoe u tellingen kunt krijgen op basis van datums in meerdere kolommen in ORACLE

Hier is een manier om dit te doen met behulp van de PIVOT-operator.

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. Store_result en get_result voor statement

  2. Oracle JDeveloper 12c gebruiken met Oracle Database 12c op Oracle Cloud Platform, deel 2

  3. Wat betekent het pipe-/veritcal-teken in TSQL?

  4. Uniek modelveld in Django en hoofdlettergevoeligheid (postgres)