I have an SQLite DB of data from a month-long festival that looks in part like this:
CREATE TABLE IF NOT EXISTS performance_status (
id INTEGER PRIMARY KEY,
name TEXT UNIQUE -- available, soldout, past, etc
)
CREATE TABLE IF NOT EXISTS performances (
id INTEGER PRIMARY KEY,
date INTEGER, -- The date of the show
sold_out_date INTEGER, -- The date on which the showing sold out
show_id INTEGER,
status_id INTEGER,
FOREIGN KEY(show_id) REFERENCES shows(id),
FOREIGN KEY(status_id) REFERENCES performance_status(id),
UNIQUE(date, show_id)
)
CREATE TABLE IF NOT EXISTS shows (
id INTEGER PRIMARY KEY,
name TEXT
)
I have a list of shows that have sold out ANY performance dates, sorted by the number of performances (days) that have sold out. Here is my query:
SELECT s.id, s.name, count(p.id) AS sellout_count
FROM shows AS s
LEFT JOIN performances AS p ON s.id = p.show_id
LEFT JOIN performance_status AS ps ON p.status_id = ps.id
WHERE ps.name = "soldout"
GROUP BY s.id
HAVING sellout_count > 0
ORDER BY sellout_count DESC
LIMIT 3
This works, and returns an array like this:
[(2100, 'Show 1', 25), (1286, 'Show 2', 25), (2936, 'Show 3', 24)]
So far so good. But I also need to know how many performance days a given show has, regardless of the sellout status. But the WHERE clause is limiting the selected rows to only those that are sold out.
If I GROUP BY s.id, ps.name, that will partition things beyond what I need, and return as many as 8 groups per show. I just want to group by the boolean "soldout or NOT soldout".
How can I do this in SQLite?