This query:
- Uses ANSI JOIN (LEFT, INNER, ...)
- Uses
LEFT JOIN
for each table in order to count Sessions IPIPs withoutInput
orDownloads
- Uses
DISTINCT
for eachCOUNT
in order to remove duplicates added by theJOIN
between tables - Counts values for totals and counts ids for unique counts
Query:
SELECT s.ip
, COUNT(DISTINCT s.id)
, COUNT(DISTINCT i.id) as TotalInputs
, COUNT(DISTINCT i.input) as UniqInputs
, COUNT(DISTINCT d.id) as files
, COUNT(DISTINCT d.shasum) as Uniqfiles
FROM sessions s
LEFT JOIN input i
ON s.id = i.session
LEFT JOIN downloads d
ON s.id = d.session
GROUP BY s.ip;
Output:
ip | COUNT(DISTINCT s.id) | TotalInputs | UniqInputs | files | Uniqfiles
IP2 | 1 | 3 | 2 | 1 | 1
IP3 | 3 | 5 | 4 | 3 | 2