I have this table :
TICKETID | PRICE | NUMBER
So for each ticketid, the player can pay a price for each number on the ticketid.
So if the player wants to pay 1$, 3$ and 4$ for numbers 22,23 and 24 for ticketid 25, then the table will look like this :
TICKETID | PRICE | NUMBER
25 | 1 | 22
25 | 3 | 23
25 | 4 | 24
I want to select a random ticket that has TOTAL PRICE >50, to make it receive a prize.
I also want that the randomization to be fair, and that when doing this draw, each ticket would have only 1 apparition rate. If I don’t use DISTINCT or GROUPBY, then a ticketid with 10 numbers will have more chances to get drawn than a ticket with 2 numbers.
I tried this but it’s not working:
SELECT DISTINCT(ticketid),SUM(price) FROM table
WHERE SUM(price)>50 GROUP BY ticketid
I get the error message
invalid usage of GROUP BY function
Can anybody help?
What you want is the “HAVING” clause which is applied to any possible candidate records AFTER the group by aggregations have been processed. The Having is applied THEN and either included (or not) in the final result set.