adding HAVING cases
[dbsrgits/SQL-Abstract.git] / etc / having-cases.sql
CommitLineData
6eaca423 1
2-- CASE 1
3SELECT users.name, SUM(commission) AS total
4 FROM commissions
5 INNER JOIN users ON ( commissions.recipient_id = users.id )
6 WHERE commissions.entry_date > '2007-01-01'
7 GROUP BY commissions.recipient_id
8 HAVING total > 500
9 ORDER BY total DESC;
10
11-- CASE 2
12SELECT users.name, aggregates.total FROM (
13 SELECT recipient_id, SUM(commission) AS total
14 FROM commissions
15 WHERE commissions.entry_date > '2007-01-01'
16 GROUP BY commissions.recipient_id
17 HAVING total > 500
18 ) AS aggregates
19INNER JOIN users ON(aggregates.recipient_id = users.id)
20ORDER BY aggregates.total DESC;
21
22-- CASE 3
23SELECT users.name, aggregates.total FROM (
24 SELECT recipient_id, SUM(commission) AS total
25 FROM commissions
26 WHERE commissions.entry_date > '2007-01-01'
27 GROUP BY commissions.recipient_id
28 ) AS aggregates
29INNER JOIN users ON(aggregates.recipient_id = users.id)
30WHERE aggregates.total > 500
31ORDER BY aggregates.total DESC