Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

SQL Tickets sold – Last 30 Days – use Where or Having?

How many tickets were sold for each route and each day, for the routes sold in the last 30 days from current day?

SELECT COUNT(TICKET_ID) NUMBER_TICKETS, ROUTE_CODE, FLIGHT_DATE 
FROM TICKETS 
WHERE (DAYS(CURRENT DATE)- DAYS(FLIGHT_DATE))<=30
GROUP BY ROUTE_CODE, FLIGHT_DATE

Should this be WHERE or HAVING? I am not sure which way is correct?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You want it in the WHERE.

Here is the best way to understand this —

The WHERE clause is used before the GROUP BY

The HAVING clause is used after the GROUP BY

So — you want to group items in the last 30 days — that happens before the group.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading