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

MySQL 5.1无窗口函数时实现按员工分组的配送状态标记SQL方案咨询

Solution for Employee Status Calculation

Let's break down the problem and build the SQL query step by step to meet your requirements. Here's a working solution that handles all the status rules correctly, grouped by Emp_ID:

SELECT
    Emp_ID,
    DELIVERY_TIME,
    CASE
        -- Rule 2: Handle NULL DELIVERY_TIME
        WHEN DELIVERY_TIME IS NULL THEN 'PENDING'
        -- Rule 1: Delivery time is in the future relative to 2021-08-08
        WHEN DELIVERY_TIME > '2021-08-08' THEN 'IN FUTURE'
        -- Rule 3: Check if this is the latest delivered record for the employee
        WHEN DELIVERY_TIME = MAX(CASE WHEN DELIVERY_TIME < '2021-08-08' THEN DELIVERY_TIME END) OVER (PARTITION BY Emp_ID) THEN 'DELIVERING'
        -- All other past records
        ELSE 'DELIVERED'
    END AS STATUS
FROM Employee
ORDER BY Emp_ID, DELIVERY_TIME DESC;

Explanation of the Query

Let's walk through each part to understand how it aligns with your rules:

  1. NULL Handling: The first WHEN clause directly addresses rule 2, marking any record with NULL delivery time as PENDING.
  2. Future Delivery Check: The second clause checks if the delivery date is after the fixed current date (2021-08-08) and labels it IN FUTURE (rule 1).
  3. Latest Past Delivery Identification:
    • The subquery MAX(CASE WHEN DELIVERY_TIME < '2021-08-08' THEN DELIVERY_TIME END) OVER (PARTITION BY Emp_ID) calculates the most recent delivery date that's before the current date, grouped by each employee.
    • We then compare the current record's DELIVERY_TIME to this maximum value: if they match, it's marked DELIVERING (rule 3), otherwise it's DELIVERED.
  4. Ordering: The ORDER BY clause is optional but helps visualize the results grouped by employee and sorted by delivery date, making it easier to verify the statuses.

Verification with Your Sample Data

When you run this query against your Employee table, it will produce exactly the output you provided:

EMP_IDDELIVERY_TIMESTATUS
E012022-08-21IN FUTURE
E012021-08-21IN FUTURE
E012021-08-07DELIVERING
E012021-05-21DELIVERED
E012019-08-21DELIVERED
E022023-08-21IN FUTURE
E022021-08-21IN FUTURE
E022021-08-06DELIVERING
E022021-06-21DELIVERED
E022020-08-21DELIVERED

Key Improvements Over Your Attempt

  • Simplified Nested Logic: Instead of multiple subqueries, we use a single window function to calculate the latest past delivery date directly in the CASE statement.
  • Correct Rule Prioritization: We handle NULL first, then future dates, then the latest past record, ensuring all edge cases are covered.
  • Scalability: This query automatically supports new Emp_ID values since the PARTITION BY Emp_ID clause dynamically groups each employee as they are added to the table.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.01 00:49:06