如何为Shop& amp; amp; amp;店主的可用性?

时间:2018-02-01 16:36:06

标签: sql-server database sql-server-2008 join relational-database

如何为Shop的日程安排创建时间段并将其映射到Shopkeepers日程表,并列出使用SQL Server可以获得店主的插槽列表。

我有2张桌子;首先是店铺开始时间和店铺关闭时间,第二张是Shopkeepers日程表。

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

如果店主按时完成工作,即从9:00到17:00工作,我就可以这样做。

问题是当店主在2班工作时。即上午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       |

1 个答案:

答案 0 :(得分: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;