使用SQL返回日期范围内每天的所有未使用信息

时间:2017-12-14 19:22:25

标签: sql ms-access

我在SQL上很糟糕。我在Access中有一张桌子,有训练师时间表。表格中的每条记录计划包含以下相关字段: 的 course_code 下, 的 training_date 下, 的 START_TIME 下, 的 END_TIME 下, 的训练者下, 的

我还有一个表 Room ,包含 room_id room_name room-capacity 。我的最终目标是返回一个查询,该查询为每个不在日程表中的日期标识每个房间。

例如,如果 Room F 计划在 12/18 12/20 12/21上使用,然后我想看看我的决赛桌显示:

12/19 | Room F | 8 capacity
12/22 | Room F | 8 capacity 

当我运行日期范围的查询时,12 / 18-12 / 22。

我很接近,但正如我所说,我的SQL不是那么热门:

SELECT Room.room_name, Room.room_capacity, Schedule.training_date
FROM Room LEFT JOIN Schedule ON Room.[room_id] = Schedule.[room] 
and (Schedule.training_date) Between [Enter Start Date] And [Enter Ending Date]
WHERE ((Schedule.room) Is Null);

在这种情况下,它产生

| Room F | 8 capacity

我只需要包含每个日期。

2 个答案:

答案 0 :(得分:0)

假设每天至少在计划中一次,您可以将其用作“日历”表:

import ais
msg = '177KQJ5000G?tO`K>RA1wUbN0TKH'   # a valid AIS encoded string
ais.decode(msg)  # this will succeed
ais.decode(msg)  # this will fail
ais.decode(msg)  # this will succeed
ais.decode(msg)  # this will fail, etc

答案 1 :(得分:0)

这显示了如何使用Date Dimension / Calendar表来获取您想要的T-SQL。 SQL的Acces实现很多更原始,但仍然可以修改以下内容。

注意:这也使用日历表来计算周末和假日。

当前时间表:

  • F室= 12 / 18,12 / 20,12 / 21
  • G室= 12 / 05,12 / 10
  • 房间H = 12 / 18,12 / 20
  • X室=没有时间表

SQL Fiddle

MS SQL Server 2014架构设置

CREATE TABLE schedule ( id int, training_date date, room int ) ;
INSERT INTO schedule ( id, training_date, room )
VALUES
      (1, '20171218', 1)
    , (2, '20171220', 1)
    , (3, '20171221', 1)
    , (4, '20171205', 3)
    , (5, '20171210', 3)
    , (6, '20171218', 4)
    , (7, '20171220', 4)
;

CREATE TABLE room ( room_id int, room_name varchar(20), room_capacity int ) ;
INSERT INTO room ( room_id, room_name, room_capacity )
VALUES 
      ( 1, 'Room F', 8 )
    , ( 2, 'Room X', 42 )
    , ( 3, 'Room G', 10 )
    , ( 4, 'Room H', 15 )
;

/* ******** MAKE SIMPLE CALENDAR TABLE ******** */

CREATE TABLE dateDim (
      theDate             DATE        NOT NULL
    , IsWeekend           BIT         DEFAULT 0
    , IsHoliday           BIT         DEFAULT 0
    , IsWorkDay           BIT         DEFAULT 0
);

/* Populate basic details of dates. */
INSERT dateDim(theDate, IsWeekend, IsHoliday) 
SELECT d
  , CONVERT(BIT, CASE WHEN DATEPART(dw,d) IN (1,7) THEN 1 ELSE 0 END)
  , CONVERT(BIT, CASE WHEN d = '20171225' THEN 1 ELSE 0 END) /* Christmas. Calc others. */
FROM (
    SELECT d = DATEADD(DAY, rn - 1, '20171201')
    FROM 
    (
        SELECT TOP (DATEDIFF(DAY, '20171201', '20171231')) 
            rn = ROW_NUMBER() OVER (ORDER BY s1.[object_id])
        FROM sys.all_objects AS s1
        CROSS JOIN sys.all_objects AS s2
        ORDER BY s1.[object_id]
    ) AS x
) AS y ;

/* If not a weekend or holiday, it's a WorkDay. */
UPDATE dateDim 
SET IsWorkDay = CASE WHEN IsWeekend = 0 AND IsHoliday = 0 THEN 1 ELSE 0 END
;

MS T-SQL查询

SELECT Room.room_name, Room.room_capacity, dateDim.theDate
FROM Room 
CROSS APPLY dateDim  /* CROSS APPLY doesn't work in Access */
LEFT JOIN Schedule ON Room.room_id = Schedule.room
  AND dateDim.theDate = Schedule.training_date
WHERE Schedule.room Is Null
  AND dateDim.isWeekend = 0
  AND dateDim.isHoliday = 0
ORDER BY dateDim.theDate, room.room_name

访问查询:我相信这个可以在Access中使用。

SELECT Room.room_name, Room.room_capacity, c1.theDate
FROM Room 
INNER JOIN ( 
  SELECT theDate 
  FROM dateDim 
  WHERE isWeekend = 0 
    AND isHoliday = 0 
    AND theDate BETWEEN '12/15/2017' AND '12/22/2017' -- To filter by your start/end dates
) c1 ON 1=1
LEFT JOIN Schedule ON Room.room_id = Schedule.room
  AND c1.theDate = Schedule.training_date
WHERE Schedule.room Is Null
ORDER BY c1.theDate, room.room_name    

<强> Results

| room_name | room_capacity |    theDate |
|-----------|---------------|------------|
|    Room F |             8 | 2017-12-01 |
|    Room G |            10 | 2017-12-01 |
|    Room H |            15 | 2017-12-01 |
|    Room X |            42 | 2017-12-01 |
|    Room F |             8 | 2017-12-04 |
|    Room G |            10 | 2017-12-04 |
|    Room H |            15 | 2017-12-04 |
|    Room X |            42 | 2017-12-04 |
|    Room F |             8 | 2017-12-05 |
|    Room H |            15 | 2017-12-05 |
|    Room X |            42 | 2017-12-05 |
|    Room F |             8 | 2017-12-06 |
|    Room G |            10 | 2017-12-06 |
|    Room H |            15 | 2017-12-06 |
|    Room X |            42 | 2017-12-06 |
|    Room F |             8 | 2017-12-07 |
|    Room G |            10 | 2017-12-07 |
|    Room H |            15 | 2017-12-07 |
|    Room X |            42 | 2017-12-07 |
|    Room F |             8 | 2017-12-08 |
|    Room G |            10 | 2017-12-08 |
|    Room H |            15 | 2017-12-08 |
|    Room X |            42 | 2017-12-08 |
|    Room F |             8 | 2017-12-11 |
|    Room G |            10 | 2017-12-11 |
|    Room H |            15 | 2017-12-11 |
|    Room X |            42 | 2017-12-11 |
|    Room F |             8 | 2017-12-12 |
|    Room G |            10 | 2017-12-12 |
|    Room H |            15 | 2017-12-12 |
|    Room X |            42 | 2017-12-12 |
|    Room F |             8 | 2017-12-13 |
|    Room G |            10 | 2017-12-13 |
|    Room H |            15 | 2017-12-13 |
|    Room X |            42 | 2017-12-13 |
|    Room F |             8 | 2017-12-14 |
|    Room G |            10 | 2017-12-14 |
|    Room H |            15 | 2017-12-14 |
|    Room X |            42 | 2017-12-14 |
|    Room F |             8 | 2017-12-15 |
|    Room G |            10 | 2017-12-15 |
|    Room H |            15 | 2017-12-15 |
|    Room X |            42 | 2017-12-15 |
|    Room G |            10 | 2017-12-18 |
|    Room X |            42 | 2017-12-18 |
|    Room F |             8 | 2017-12-19 |
|    Room G |            10 | 2017-12-19 |
|    Room H |            15 | 2017-12-19 |
|    Room X |            42 | 2017-12-19 |
|    Room G |            10 | 2017-12-20 |
|    Room X |            42 | 2017-12-20 |
|    Room G |            10 | 2017-12-21 |
|    Room H |            15 | 2017-12-21 |
|    Room X |            42 | 2017-12-21 |
|    Room F |             8 | 2017-12-22 |
|    Room G |            10 | 2017-12-22 |
|    Room H |            15 | 2017-12-22 |
|    Room X |            42 | 2017-12-22 |
|    Room F |             8 | 2017-12-26 |
|    Room G |            10 | 2017-12-26 |
|    Room H |            15 | 2017-12-26 |
|    Room X |            42 | 2017-12-26 |
|    Room F |             8 | 2017-12-27 |
|    Room G |            10 | 2017-12-27 |
|    Room H |            15 | 2017-12-27 |
|    Room X |            42 | 2017-12-27 |
|    Room F |             8 | 2017-12-28 |
|    Room G |            10 | 2017-12-28 |
|    Room H |            15 | 2017-12-28 |
|    Room X |            42 | 2017-12-28 |
|    Room F |             8 | 2017-12-29 |
|    Room G |            10 | 2017-12-29 |
|    Room H |            15 | 2017-12-29 |
|    Room X |            42 | 2017-12-29 |

<强> Access-method Results

| room_name | room_capacity |    theDate |
|-----------|---------------|------------|
|    Room F |             8 | 2017-12-15 |
|    Room G |            10 | 2017-12-15 |
|    Room H |            15 | 2017-12-15 |
|    Room X |            42 | 2017-12-15 |
|    Room G |            10 | 2017-12-18 |
|    Room X |            42 | 2017-12-18 |
|    Room F |             8 | 2017-12-19 |
|    Room G |            10 | 2017-12-19 |
|    Room H |            15 | 2017-12-19 |
|    Room X |            42 | 2017-12-19 |
|    Room G |            10 | 2017-12-20 |
|    Room X |            42 | 2017-12-20 |
|    Room G |            10 | 2017-12-21 |
|    Room H |            15 | 2017-12-21 |
|    Room X |            42 | 2017-12-21 |
|    Room F |             8 | 2017-12-22 |
|    Room G |            10 | 2017-12-22 |
|    Room H |            15 | 2017-12-22 |
|    Room X |            42 | 2017-12-22 |