【问题标题】:Not within ranges from another table不在另一个表的范围内
【发布时间】:2011-08-29 14:26:02
【问题描述】:

我正在使用 MSSQL 2000。

我有一个包含日期列的表格,t_mydates(dateA)。

我有第二张表 t_exlusions(start_date,end_date)

我想要一个查询,它返回来自 t_mydates 的所有日期,这些日期不在 t_exlusions 的任何范围之间。

【问题讨论】:

  • 你试过什么?你被困在哪里了?为了获取不存在排除的记录,NOT EXISTS 会自然而然地浮现在脑海中。

标签: sql sql-server tsql


【解决方案1】:

查询:

select d.*
from @t_mydates d
left join @t_exlusions e on date between start_date and end_date
where start_date is null and end_date is null

考试日期:

declare @t_mydates table (date date)
insert into @t_mydates (date)
values
(GETDATE()),(GETDATE()-1),(GETDATE()-2),(GETDATE()-3),(GETDATE()-4),(GETDATE()-5),(GETDATE()-6),(GETDATE()-7)

declare @t_exlusions table (start_date date, end_date date)
insert into @t_exlusions (start_date, end_date)
values
(GETDATE()-1, GETDATE()), (GETDATE()-5, GETDATE()-4)

结果:

2011-08-27
2011-08-26
2011-08-23
2011-08-22

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-23
    • 1970-01-01
    相关资源
    最近更新 更多