如何为商店的日程安排和店主的空闲时间创建时间段?

How can I create time slots for Shop's schedule & Shopkeeper's Availability?

如何使用 SQL 服务器为 Shop 的时间表创建时间段并将其映射到 Shopkeepers 时间表,并获得店主可用的时间段列表。

我有2个table;首先是商店开始时间和商店关闭时间,第二个 table 是店主时间表。

我想创建 15 分钟的时间段并标记店主是否有空。

如果店主正好按时工作,即从 9:00 到 17:00 轮班工作,我可以做到这一点。

问题出在店主两班倒的时候。即从上午 9 点到中午 12 点,然后是下午 2 点到 4 点。

提前致谢。

商店时间表:

id | Shop Open  | Shop Close    | ShopId    |
---+------------+---------------+-----------+
1  | 900        | 1800          |  1        |
2  | 900        | 1730          |  2        |
3  | 830        | 1600          |  3        |
4  | 845        | 1630          |  4        | 

店主时间表:

ShopkeeperId | Shopkeeper Start     | Shopkeeper End    | ShopId    |
-------------+----------------------+-------------------+-----------+
1            | 900                  | 1800              |  1        |
2            | 900                  | 1200              |  1        |
2            | 1400                 | 1700              |  1        |
3            | 830                  | 1600              |  2        |
4            | 845                  | 1630              |  2        | 
4            | 845                  | 1630              |  2        |

日程安排:

ShopkeeperId    | ShopId  | ShopSlot Start  | ShopSlot Start    | Shopkeeper Avialable  |
----------------+---------+-----------------+-------------------+-----------------------+
2               | 1       | 900             |  915              |   1           |
2               | 1       | 915             |  930              |   1           |
2               | 1       | 930             |  1000             |   1           |
.
.
.

2           | 1         | 1200          |  1215             |       0           |
2           | 1         | 1215          |  1230             |       0           |
2           | 1         | 1230          |  1240             |       0           |
.
.
.
2           | 1         | 1400          |  1415         |   1       |
2           | 1         | 1415          |  1430         |   1       |
2           | 1         | 1430          |  1445         |   1       |

这是一种使用各种临时表生成计划的方法:

-- ================================================
-- GIVEN DATA - Table of ShopKeeper Schedule 
CREATE TABLE #SKSchedule (
    ShopKeeperId int
    , ShopKeeperStart int
    , ShopKeeperEnd int
    , ShopID int
    );

INSERT INTO #SKSchedule VALUES
(1            , 900                  , 1800              ,  1        )
,(2            , 900                  , 1200              ,  1        )
,(2            , 1400                 , 1700              ,  1        )
,(3            , 830                  , 1600              ,  2        )
,(4            , 845                  , 1630              ,  2        )
,(5           , 845                  , 1630              ,  4       )
;

-- ================================================
-- GIVEN DATA - Table of Shop opening times
CREATE TABLE #ShopOpenClose(
    ID int
    , ShopOpen int
    , ShopClose int
    , ShopID int
    );
INSERT INTO #ShopOpenClose VALUES
(1  , 900        , 1800      ,  1            )
,(2  , 900        , 1730      ,  2            )
,(3  , 830        , 1600      ,  3            )
,(4  , 845        , 1630      ,  4            );



-- ================================================
-- Extract a list of shopkeeper/shop combos into ShopkeeperShop table
CREATE TABLE #SKSHOP(
    ShopKeeperID int
    , ShopID int
    )
INSERT INTO #SKSHOP SELECT DISTINCT ShopkeeperID, ShopID from #SKSchedule;


-- ================================================
-- Create a table of all possible time slots in a day
CREATE TABLE #TIMESLOTS (
     TSStart int
    , TSEnd int
    );

    -- Variables for the four quarter-hours in an hour, starting at 0900
    DECLARE @AA int = 900, @BB int = 915, @CC int = 930, @DD int = 945, @EE int = 1000;
    DECLARE @PP int = 0;

    -- Loop to create 10 hours 09-10 through 18-19
    WHILE @PP < 900
    BEGIN
        INSERT INTO #TIMESLOTS  

        VALUES 
            ( @AA + @PP, @BB + @PP ) , (@BB + @PP, @CC + @PP ), ( @CC + @PP, @DD + @PP ), (@DD + @PP, @EE + @PP );

        SET @PP = @PP + 100;
    END;


-- ================================================
-- Create a table of all possible timeslots per shopkeeper/shop, taking
-- into account the shop opening hours

SELECT SKS.ShopKeeperID
    , SKS.ShopID
    , TSL.TSStart ShopSlotStart
    , TSL.TSEnd ShopSlotEnd
    INTO #SKSHTS
    FROM #SKSHOP SKS
    CROSS JOIN #TIMESLOTS TSL;

    -- Remove those timeslots that are outside the shop opening hours range
    DELETE SKS FROM
        #SKSHTS SKS
        JOIN #ShopOpenClose SOC
         ON 
            SKS.ShopID = SOC.ShopID
         AND (SKS.ShopSlotStart < SOC.ShopOpen
         OR SKS.ShopSlotEnd > SOC.ShopClose);




-- ================================================
-- Now report all the shopslots indicating shopkeepers'
-- availability based on the shopkeepers' times
SELECT 
     TSLT.ShopKeeperID
    , TSLT.ShopID
    , TSLT.ShopslotStart
    , TSLT.ShopslotEnd
    , CASE WHEN SSCH.ShopKeeperID IS NULL THEN 0 ELSE 1 END ShopKeeperAvailable

    FROM 
        #SKSHTS TSLT
        LEFT JOIN #SKSchedule SSCH          
        ON TSLT.ShopID = SSCH.ShopID
            AND TSLT.ShopKeeperID = SSCH.ShopkeeperID
            AND TSLT.ShopslotStart >= SSCH.ShopKeeperStart 
            AND TSLT.ShopslotEnd <= SSCH.ShopKeeperEnd 

    ORDER BY ShopID, ShopSlotStart, ShopKeeperID;