I have a list of TV shows. Each TV show may be blacked out in 0 or more timezones. To say that a show is "blacked out" in a timezone means that the network does not have rights to air the show in that timezone. This data looks like this:
|----|---------------------|
| ID | Show |
|----|---------------------|
| 1 | Nightly News |
| 2 | Primetime Sitcom |
| 3 | Daytime Talkshow |
| 4 | Nightly News II |
| 5 | Daytime Talkshow II |
| 6 | Nightly News III |
|----|---------------------|
|
|-----join
|
v
|----|----------------------|
| ID | Timezone Restriction |
|----|----------------------|
| 1 | EST |
| 1 | CST |
| 1 | PST |
| 2 | EST |
| 2 | CST |
| 3 | PST |
| 5 | CST |
| 5 | PST |
| 6 | HST |
|----|----------------------|
Not all shows are timezone restricted (most are not). Given this data, I need to fetch a list contains as many results as necessary in order to supply 2 shows in each timezone that are not blacked out. The results should be ordered by ID, with each timezone seeing the lowest possible unrestricted IDs.
For instance, in the above dataset, this hypothetical query would return rows 1-4, e.g:
|----|------------------|--------------|
| ID | Show | Restrictions |
|----|------------------|--------------|
| 1 | Nightly News | EST,CST,PST |
| 2 | Primetime Sitcom | EST,CST |
| 3 | Daytime Talkshow | PST |
| 4 | Nightly News II | None |
|----|------------------|--------------|
As you can see, in the above result set, all timezones have at least 2 shows which are unrestricted. A viewer in EST or CST could watch programs 3 and 4. A viewer in PST could view programs 2 and 4. A viewer in MST or HST could view programs 1 and 2.
I can't for the life of me figure out the SQL that would get at this problem (sidenote, I don't actually need the "restrictions" column in my result, that's just here for explanatory purposes).