'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?
Solution 1:[1]
where is the right command, consider to use "datediff" for calculate 30 days
Solution 2:[2]
the WHERE clause is used to specify a condition for filtering records before any groupings are made the HAVING clause is used to specify a condition for filtering values from a group
in this case, the where clause is used for filtering the condition, not the group. You should use "having",for example, if you wanted to check the ticket_id.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Simone Gosetto |
| Solution 2 | mikasa |
