如何通过记录应该同时存在于匹配项中而不存在于另一个匹配项中来获取组的结果
How to get result of a group by where records should both exist in a match and not exists in another match
问题:
我需要进行 SELECT 查询 returns shift_id
其中
day_of_week
是 IN (0,1,2,3,4)
和 NOT IN (5,6)
,
如:
"Find shift where shift days are Sunday through Thursday (and no records exist for Friday-Saturday)"
数据库结构:
Table shifts
:
shift_id - int, primary key
... - other columns
Table shift_days
:
shift_day_id - int, primary key
shift_id - int, foreign key to shifts table
day_of_week - int, where 0 = Sunday, 1 = Monday,..., 6 - Saturday
... - more columns
例如
- 如果班次是周一到周五,那么
shift_days
只有5条记录day_of_week
从1到5
- 如果班次是星期天 - 星期四,那么
shift_days
只有 5 条记录 day_of_week
从 0 到 4
数据库小提琴:
P.S.
我能够提出这个查询:
SELECT shift_id
FROM (
SELECT shift_id, STRING_AGG(day_of_week, ',') AS 'days_of_week'
FROM shift_days
GROUP BY shift_id
) AS sdw
WHERE sdw.days_of_week = '0,1,2,3,4'
但我不知道如何保证顺序升序为“0,1,2,3,4”,我想看看是否有其他方法可以使用 STRING_AGG 以防万一我想用于不同的 SQL 方言。
P.P.S.
再次强调,匹配 day_of_week (5,6) 的任何移位都是错误匹配,因为我想排除这些记录。
我想包括 (0,1,2,3,4) 条记录,这些记录必须全部为 0-4。
group by shift_id
having
count(case when day_of_week in (5, 6) then 1 end) = 0
and count(distinct day_of_week) = 5
可以尝试以下查询:
如果您希望所有班次都没有 day_of_week 5,6
SELECT DISTINCT shift_id
FROM shift_days S
WHERE NOT EXISTS (SELECT 1 FROM shift_days T WHERE T.shift_id=S.shift_id AND
T.day_of_week in (5, 6))
如果您希望所有班次都有 day_of_week 0,1,2,3,4
SELECT shift_id FROM shift_days S
WHERE NOT EXISTS (SELECT 1 FROM shift_days T WHERE T.shift_id=S.shift_id AND
T.day_of_week in (5, 6))
and day_of_week in (0,1,2,3,4)
group by shift_id having count(1)=5
问题:
我需要进行 SELECT 查询 returns shift_id
其中
day_of_week
是 IN (0,1,2,3,4)
和 NOT IN (5,6)
,
如:
"Find shift where shift days are Sunday through Thursday (and no records exist for Friday-Saturday)"
数据库结构:
Table shifts
:
shift_id - int, primary key ... - other columns
Table shift_days
:
shift_day_id - int, primary key shift_id - int, foreign key to shifts table day_of_week - int, where 0 = Sunday, 1 = Monday,..., 6 - Saturday ... - more columns
例如
- 如果班次是周一到周五,那么
shift_days
只有5条记录day_of_week
从1到5 - 如果班次是星期天 - 星期四,那么
shift_days
只有 5 条记录day_of_week
从 0 到 4
数据库小提琴:
P.S. 我能够提出这个查询:
SELECT shift_id
FROM (
SELECT shift_id, STRING_AGG(day_of_week, ',') AS 'days_of_week'
FROM shift_days
GROUP BY shift_id
) AS sdw
WHERE sdw.days_of_week = '0,1,2,3,4'
但我不知道如何保证顺序升序为“0,1,2,3,4”,我想看看是否有其他方法可以使用 STRING_AGG 以防万一我想用于不同的 SQL 方言。
P.P.S.
再次强调,匹配 day_of_week (5,6) 的任何移位都是错误匹配,因为我想排除这些记录。 我想包括 (0,1,2,3,4) 条记录,这些记录必须全部为 0-4。
group by shift_id
having
count(case when day_of_week in (5, 6) then 1 end) = 0
and count(distinct day_of_week) = 5
可以尝试以下查询:
如果您希望所有班次都没有 day_of_week 5,6
SELECT DISTINCT shift_id
FROM shift_days S
WHERE NOT EXISTS (SELECT 1 FROM shift_days T WHERE T.shift_id=S.shift_id AND
T.day_of_week in (5, 6))
如果您希望所有班次都有 day_of_week 0,1,2,3,4
SELECT shift_id FROM shift_days S
WHERE NOT EXISTS (SELECT 1 FROM shift_days T WHERE T.shift_id=S.shift_id AND
T.day_of_week in (5, 6))
and day_of_week in (0,1,2,3,4)
group by shift_id having count(1)=5