【问题标题】:Select Statement Returning Multiple of Same Dates选择返回多个相同日期的语句
【发布时间】:2015-04-16 19:54:27
【问题描述】:

我遇到了返回多个相同日期的 select 语句的问题。

我将从我拥有的表格开始:

日历:非常基本,只是一张包含未来 20 年所有日子的表格。

PKDate
------
2015-04-01
2015-04-02
2015-04-03
etc...

DaysWorked:此表包含公司所有设备已工作的所有天数。日历中的PKDate 与此表中的DayWorked 存在外键约束。

DayWorked  |  Unit
----------------
2015-04-01 | 102
2015-04-05 | 103

事件:这是我们调度系统背后的表。这包含所有可以作为休息日预订的日子。用户可以选择休息日或假期的开始和结束日期。此表中没有外键约束。

Name     |  EventStart  |  EventEnd  |  Unit  
-----------------------------------------
Days Off | 2015-04-06   | 2015-04-08 | 103
Days Off | 2015-04-03   | 2015-04-09 | 102

这是我正在执行的存储过程:

select distinct PKDate as 'Date', case when PKDate not in (select DayWorked
                                             from DaysWorked
                                             where Unit='124')
                                then 'AVAILABLE'
                                else ''
                                end
                                as 'Available',

                    case when PKDate in (select DayWorked
                                        from DaysWorked
                                        where Unit='124')
                    then 'WORKED'
                    else ''
                    end
                    as 'Worked',

                    case when PKDate between E.EventStart and DATEADD(day, -1, E.EventEnd)
                         and E.ResourceID='124'
                    then UPPER(E.Name)
                    else ''
                    end
                    as 'Schedule'
from Event E
full outer join Calendar C
on PKDate between E.EventStart and E.EventEnd
where PKDate between '2015-04-01' and GETDATE()
order by PKDate asc

这个存储过程几乎按计划工作。我希望程序的结果在日历中的每一天(日期)中显示,然后显示设备是否可用(可用)、设备是否工作(工作)以及设备是否已预订休息日或假期(时间表)。

运行程序时发生的情况是同一日期的日期显示不止一次。一个例子如下图所示:

在 4 月 13 日至 4 月 16 日这几天重复。我相信这些日子会重复,因为我在事件表中有那些日子的东西,但我不知道为什么这一天会显示两次。我怎样才能让这些天只显示一次?

【问题讨论】:

    标签: sql-server date case


    【解决方案1】:
    select C.PKDate
      ,case when not exists ( select * from DaysWorked where Unit = '124' and DayWorked = C.PKDate )
        and not exists ( select * from Event E where E.EventStart <= C.PKDate and E.EventEnd >= C.PKDate and E.ResourceID = '124')  
        then 'AVAILABLE' else '' end as Available
      ,case when exists ( select * from DaysWorked where Unit = '124' and DayWorked = C.PKDate ) then 'WORKED' else '' end as Worked
      ,isnull((select max(E.Name) from Event E where E.EventStart <= C.PKDate and E.EventEnd >= C.PKDate and E.ResourceID = '124' ), '') as Schedule
    from Calendar C
    where C.PKDate between '2015-4-1' and getdate()
    order by c.PKDate
    

    【讨论】:

    • 对于日期,这是我正在寻找的。但它不包括 13-16 的时间表列
    • 这行得通,但是当Schedule 列中有数据时,Available 列中不能有任何内容。
    • 好的,如果有任何安排的活动,我在可用列中添加了一个额外的子句,使其为空
    • 哦,我明白了,您使用相同的子句来检查休息日。我本可以得到最后一部分啊哈。谢谢您的帮助。我一直被困在这上面!!!
    猜你喜欢
    • 2019-01-30
    • 1970-01-01
    • 2012-12-12
    • 2017-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多