I have a tickets
table with this info:
ticket_id | user_id | used_at
----------------------------------------
xxx | xxx | datetime
xxx | xxx | datetime
xxx | xxx | datetime
I want to get a list of all users who has ONLY ONE TICKET and USED_AT date of it.
My current select works to get user and their first ticket date.
SELECT
washtickets.id,
washtickets.user_id,
washtickets.used_at
FROM
washtickets,
(SELECT user_id, min(used_at) AS used_at FROM washtickets GROUP BY user_id) max_user
WHERE
washtickets.deleted_at IS NULL
AND washtickets.user_id = max_user.user_id
AND washtickets.used_at = max_user.used_at
ORDER BY
used_at ASC;
But I don’t know to add COUNT(*) to filter only users who has ONLY ONE used ticket.
Any help?
2
Answers
First JOINS are SQL Standard for over 20 years, so please use them
Count
is an aggregation function, so you need aGROUP BY
All columns that are not in the
GROUP BY
must have a aggregation function, as you only want people with one ticket, so the id should be have one numbers so the aggregation function doesn’t matter.Maybe I am misunderstanding what you are trying to do, but I do not see the need for the subquery.
It is not completely clear what you want as you start off by saying –
but later say –
Either way I cannot see the need (or benefit) in the subquery.
This query returns all users with only one ticket which is not deleted and has a
used_at
value:Here’s a db<>fiddle.