Using MSSQL
I have a user table and a table of products to which they are subscribed. These subscriptions are free (F) or paid (P). I joined the tables, converted the F / P value to a numerical value using the case statement, and then summed these values by user ID, assuming that anyone with only free accounts will have a sum of 0, those that have at least would have one paid account of 1 or more. I got this far from the following:
SELECT t1.user_id, SUM(
CASE
WHEN t2.free_paid = 'P'
THEN 1
ELSE 0
END ) as highest
FROM users t1 INNER JOIN accounts t2
ON t1.user_id = t2.user_id
WHERE t2.account = 'A'
GROUP BY t1.user_id
ORDER BY t1.user_id
This gives a result like:
755 2
1259 2
2031 1
3888 0
This means that all but 3888 have at least one paid account.
-, , (3 ) , (1 ).
, . @free @paid case , , .
?