用于查找日期范围内缺失 ID 的 T-SQL 查询
给定这些表
table Channel
--------------
ChannelID int IDENTITY
<other irrelevant stuff>
table Program
--------------
ProgramID int IDENTITY
ChannelID int
AiringDate datetime
<other irrelevant stuff>
和这个查询
SELECT C.ChannelID, t.AiringDate
FROM
Channel C
LEFT JOIN (
SELECT distinct ChannelID
FROM Program
WHERE AiringDate = '2010-01-16'
) p
ON p.ChannelID=C.ChannelID
CROSS JOIN (
SELECT AiringDate = '2010-01-16'
) t
WHERE C.ChannelID IN (1, 2, 74, 15, 906) /* the Channel table contains more channels than we are interested in */
AND p.ChannelID IS NULL
,
ChannelID | AiringDate
----------|-----------
2 | 2010-01-16
906 | 2010-01-16
我如何修改它以接受日期范围,以便结果将类似于
ChannelID | AiringDate
----------|-----------
2 | 2010-01-16
906 | 2010-01-16
2 | 2010-01-17
906 | 2010-01-17
如果这两天中的任何一天在这两个频道上都没有播出节目
这不会返回任何行
SELECT C.ChannelID, t.AiringDate
FROM
Channel C
LEFT JOIN (
SELECT distinct ChannelID, AiringDate
FROM Program
WHERE AiringDate between '2010-01-16' and '2010-01-17'
) p
ON p.ChannelID=C.ChannelID
CROSS JOIN (
SELECT AiringDate = '2010-01-16'
union
SELECT AiringDate = '2010-01-17'
) t
WHERE C.ChannelID IN (1, 2, 74, 15, 906)
AND p.ChannelID IS NULL
CROSS JOIN 有点难看,最好完全摆脱它。请注意,第一个示例查询有点复杂;对于单个日期,我有一个更简单的日期,仅输出缺少的 ChannelID:
SELECT C.ChannelID
FROM
Channel C
LEFT JOIN (
SELECT distinct ChannelID
FROM Program
WHERE AiringDate = '2010-01-16'
) p
ON p.ChannelID=C.ChannelID
WHERE C.ChannelID IN (1, 2, 74, 15, 906)
AND p.ChannelID IS NULL
Given these tables
table Channel
--------------
ChannelID int IDENTITY
<other irrelevant stuff>
table Program
--------------
ProgramID int IDENTITY
ChannelID int
AiringDate datetime
<other irrelevant stuff>
and this query
SELECT C.ChannelID, t.AiringDate
FROM
Channel C
LEFT JOIN (
SELECT distinct ChannelID
FROM Program
WHERE AiringDate = '2010-01-16'
) p
ON p.ChannelID=C.ChannelID
CROSS JOIN (
SELECT AiringDate = '2010-01-16'
) t
WHERE C.ChannelID IN (1, 2, 74, 15, 906) /* the Channel table contains more channels than we are interested in */
AND p.ChannelID IS NULL
which yields
ChannelID | AiringDate
----------|-----------
2 | 2010-01-16
906 | 2010-01-16
how can I modify it to accept a date range, so that the result will be something like
ChannelID | AiringDate
----------|-----------
2 | 2010-01-16
906 | 2010-01-16
2 | 2010-01-17
906 | 2010-01-17
if there were no programs aired on these 2 channels any of these two days
This returns no rows
SELECT C.ChannelID, t.AiringDate
FROM
Channel C
LEFT JOIN (
SELECT distinct ChannelID, AiringDate
FROM Program
WHERE AiringDate between '2010-01-16' and '2010-01-17'
) p
ON p.ChannelID=C.ChannelID
CROSS JOIN (
SELECT AiringDate = '2010-01-16'
union
SELECT AiringDate = '2010-01-17'
) t
WHERE C.ChannelID IN (1, 2, 74, 15, 906)
AND p.ChannelID IS NULL
That CROSS JOIN
is a bit ugly, and it would be nice to get rid of it altogether. Note that the first example query is a bit convoluted; For single dates I have a simpler one that only outputs missing ChannelIDs:
SELECT C.ChannelID
FROM
Channel C
LEFT JOIN (
SELECT distinct ChannelID
FROM Program
WHERE AiringDate = '2010-01-16'
) p
ON p.ChannelID=C.ChannelID
WHERE C.ChannelID IN (1, 2, 74, 15, 906)
AND p.ChannelID IS NULL
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我理解正确的话,这应该得到你的要求。
LEFT JOIN
删除所有已具有 AiringDates 的频道SQL 语句
If I understand correct, this should get what you requested.
LEFT JOIN
to remove all Channels already having AiringDatesSQL Statement