Please can one help me in creating MTD,YTD ,QTD SQL.

Database
Enthusiast

Please can one help me in creating MTD,YTD ,QTD SQL.

Please can one help me in creating MTD,YTD ,QTD SQL.

1 REPLY
Junior Contributor

Re: Please can one help me in creating MTD,YTD ,QTD SQL.

You can use TRUNC to get the 1st of month/quarter/year:

TRUNC(CURRENT_DATE, 'MM')

TRUNC(CURRENT_DATE, 'Q')

TRUNC(CURRENT_DATE, 'Y')

 

And then it's conditional aggregation:

SUM(CASE WHEN datecol BETWEEN TRUNC(CURRENT_DATE, 'MM') AND CURRENT_DATE THEN col ELSE 0 END)

 

Dieter