You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于时间范围匹配的工单表与班次表关联查询方案

Got it, let's figure out how to map your work orders to their correct shifts. First, let's clarify the tables you're working with (I've formatted them into readable tables for clarity):

Work Orders Table

DateTimeOfEntryPlantManufacturingLineOrderNumber
2017-06-1311:56:583120D19100015234
2017-06-1312:12:183120MIX100016098
2017-06-1312:17:593120D16100015218
2017-06-1312:21:013120D19100015234
2017-06-1312:22:233120D19100016017
2017-06-1312:43:523120WW2100015543
2017-06-1312:45:493120WW2100015543
2017-06-1313:00:263120W43100015574
2017-06-1313:01:513120PRE100016148
2017-06-1313:05:533120MIX100016095

Shifts Table

PlantShiftStartTimeEndTime
3101Day06:01:00.000000014:00:00.0000000
3120Day06:01:00.000000014:00:00.0000000
3150Day06:01:00.000000014:00:00.0000000
3160Day06:01:00.000000014:00:00.0000000
3170Day06:01:00.000000014:00:00.0000000
3101Afternoon14:01:00.000000022:00:00.0000000
3120Afternoon14:01:00.000000022:00:00.0000000
3150Afternoon14:01:00.000000022:00:00.0000000
3160Afternoon14:01:00.000000022:00:00.0000000
3170Afternoon14:01:00.000000022:00:00.0000000
3101Night22:01:00.000000006:00:00.0000000
3120Night22:01:00.000000006:00:00.0000000
3150Night22:01:00.000000006:00:00.0000000
3160Night22:01:00.000000006:00:00.0000000
3170Night22:01:00.000000006:00:00.0000000

Implementation Solution

The key challenge here is handling the overnight night shift (where StartTime is later than EndTime). Here's a SQL query that works across most databases, with adjustments for your table structure:

For SQL Server

SELECT 
    wo.Date,
    wo.TimeOfEntry,
    wo.Plant,
    wo.ManufacturingLine,
    wo.OrderNumber,
    s.Shift AS ShiftName
FROM 
    WorkOrders wo
JOIN 
    Shifts s ON wo.Plant = s.Plant
WHERE 
    -- Handle regular shifts (Day/Afternoon: StartTime <= EndTime)
    (s.StartTime <= s.EndTime 
     AND CAST(CAST(wo.Date AS DATETIME) + CAST(wo.TimeOfEntry AS DATETIME) AS TIME) 
         BETWEEN s.StartTime AND s.EndTime)
    -- Handle overnight Night shifts (StartTime > EndTime)
    OR (s.StartTime > s.EndTime 
        AND (CAST(CAST(wo.Date AS DATETIME) + CAST(wo.TimeOfEntry AS DATETIME) AS TIME) >= s.StartTime 
             OR CAST(CAST(wo.Date AS DATETIME) + CAST(wo.TimeOfEntry AS DATETIME) AS TIME) <= s.EndTime))
ORDER BY 
    wo.Date, wo.TimeOfEntry;

For MySQL

SELECT 
    wo.Date,
    wo.TimeOfEntry,
    wo.Plant,
    wo.ManufacturingLine,
    wo.OrderNumber,
    s.Shift AS ShiftName
FROM 
    WorkOrders wo
JOIN 
    Shifts s ON wo.Plant = s.Plant
WHERE 
    -- Regular shifts
    (s.StartTime <= s.EndTime 
     AND TIME(CONCAT(wo.Date, ' ', wo.TimeOfEntry)) 
         BETWEEN s.StartTime AND s.EndTime)
    -- Overnight shifts
    OR (s.StartTime > s.EndTime 
        AND (TIME(CONCAT(wo.Date, ' ', wo.TimeOfEntry)) >= s.StartTime 
             OR TIME(CONCAT(wo.Date, ' ', wo.TimeOfEntry)) <= s.EndTime))
ORDER BY 
    wo.Date, wo.TimeOfEntry;

Logic Explanation

  1. Plant Matching: We first join the two tables on the Plant field to ensure we only look at shifts relevant to the work order's plant.
  2. Regular Shifts: For Day and Afternoon shifts (where StartTime is earlier than EndTime), we check if the work order's time falls directly between the shift's start and end times.
  3. Overnight Shifts: For Night shifts (which span midnight), we check if the work order's time is either after the shift starts (22:01+) or before the shift ends (06:00-). This accounts for the shift crossing into the next day.

This query will correctly assign every work order to its corresponding shift, including edge cases like entries right at shift boundaries.

内容的提问来源于stack exchange,提问作者Anton Fernando

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 09:07:49