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

Recupera i dati in base al mese di un trimestre in sql

Hai bisogno di una query come questa. Assicurati di includere NULL controlla Data1 ,Data2 quando mancano record per date future o passate e impostarlo su zero se necessario.

select To_CHAR(Month,'MON-YY') Month,
        CASE 
                WHEN MOD ( EXTRACT ( MONTH FROM Month ), 3 ) = 1 
                THEN Data2 + LEAD ( Data2 ,1) OVER ( ORDER BY Month ) + LEAD ( Data2 ,2) OVER ( ORDER BY Month )
                WHEN MOD ( EXTRACT ( MONTH FROM Month ), 3 ) = 2 
                THEN LAG(Data1,1) OVER ( ORDER By Month ) + Data2 + LEAD ( Data2 ,1) OVER ( ORDER BY Month )
                WHEN MOD ( EXTRACT ( MONTH FROM Month ), 3 ) = 0 
                THEN LAG(Data1,2) OVER ( ORDER By Month ) + LAG(Data1,1) OVER ( ORDER By Month )+Data2
        END data
FROM 
        Table1;

SQLFiddle