如何跳过假日代码= 1的日期

时间:2009-10-07 05:33:39

标签: sql sql-server tsql sql-server-2000

使用SQL Server 2000

表1

Date        Holiday

23-02-2009    0 
24-02-2009    1
25-02-2009    0 

表2

ID Date

01 23-02-2009     
01 24-02-2009    
02 25-02-2009     
…,

这里我想跳过Holiday = 1

的日期
Expected Output

01 23-02-2009
02 25-02-2009

如何查询此情况?

4 个答案:

答案 0 :(得分:2)

选择:

SELECT t2.id, 
       t2.date
  FROM TABLE_2 t2
  JOIN TABLE_1 t1 ON t1.date = t2.date
                 AND t1.holiday = 0

......或使用EXISTS:

SELECT t2.id, 
       t2.date
  FROM TABLE_2 t2
 WHERE EXIST(SELECT NULL
              FROM TABLE_1 t1
             WHERE t1.date = t2.date
               AND t1.holiday = 0)

答案 1 :(得分:0)

不知道您正在使用哪种数据类型,但乍一看它看起来很简单:

select Table2.ID, Table2.Date from 
Table2 inner join Table1 on Table2.Date = Table1.Date
where Table1.Holiday <> 1

select Table2.ID, Table2.Date
from Table2
Where Table2.Date not in (Select Date From Table1 Where Holiday = 1)

答案 2 :(得分:0)

试试这个

SELECT  *
FROM    TABLE2 t2 
WHERE   t2.Date NOT IN (SELECT  Date FROM TABLE1 WHERE Holiday = 1)

如果表没有标记假期的其他值,为什么有假日列,为什么日期不是假期?

我们有一个假期表

CREATE TABLE [dbo].[METADATA_CALENDAR](
    [CalendarDate] [datetime] NOT NULL,
    [Description] [nvarchar](500) NULL,
    [CalendarType] [nvarchar](100) NOT NULL,
    [CalendarLocal] [nvarchar](50) NOT NULL,
)

可以为您提供各种日历类型以及各个国家/地区。

答案 3 :(得分:0)

不了解数据库供应商的具体细节,但有类似的内容:

select t2.date 
from table2 t2 
where t2.date in (select t1.date 
                  from table1 t1 
                  where t1.holiday <> 1)