1

我无法创建一个高效的 MySQL 查询,该查询根据特定规则计算准时交货。如果 PHP 解决方案更有效,我也愿意接受它。我还在学习,所以请放轻松。

这是我的工作表:

工作表:

+--------------+
|  Field       |
+--------------+
|  Job_ID      | 
|  Customer_ID |
|  Rep_ID      |<- Used for Filtering
|  Premium     |<- Tinyint(1) (0 by default, 1 if Premium)
|  Due_Date    |
|  Ship_Date   |
+--------------+

以下是计算准时交货的规则:

*Working Day = Weekdays (I would like to exclude holidays but for now, let's just go with all Weekdays)

Rule 1: (Simple) If a Job is of the Premium Type:
    Ship_Date before Due_Date = Early
    Ship_Date same as Due_Date = On Time
    Ship_Date after Due_Date = Late

Rule 2: (Complex) If a Job is of the Standard Type:
    Ship_Date before Due_Date minus 2 Working Days = Early
    Ship_Date within Due_Date minus 2 Working Days and Due_Date plus 1 Working Day = On Time
    Ship_Date after Due_Date plus 1 Working Day = Late

Example:
   Due_Date=Nov 9, 2012 and Premium=0
   if Ship_Date is Nov 1, 2012 then Status is Early by (4) Days
   if Ship_Date is Nov 6, 2012 then Status is Early by (1) Day
   if Ship_Date is Nov 7, 2012 then Status is On Time 
   if Ship_Date is Nov 12, 2012 then Status is On Time 
   if Ship_Date is Nov 13, 2012 then Status is Late by (1) Day
   if Ship_Date is Nov 20, 2012 then Status is Late by (6) Days

所需的报告格式示例:我试图在标题处获取早期、准时和延迟作业的数量。订单以发货日期为准。

On Time Delivery Report    

October (Early: 1 / On Time: 2 / Late: 0) 
+------+-----------+---------+----------+-----------+-----------+------+--------+ 
| Job #| Cust Name | Premium | Due_Date | Ship_Date |Qty Shipped|Status|DaysDiff|
+------+-----------+---------+----------+-----------+-----------+------+--------+
 <List all jobs shipped in October> 


November (Early: 0 / On Time: 3 / Late: 0)
+------+-----------+---------+----------+-----------+-----------+------+--------+ 
| Job #| Cust Name | Premium | Due_Date | Ship_Date |Qty Shipped|Status|DaysDiff|
+------+-----------+---------+----------+-----------+-----------+------+--------+
 <List all jobs shipped in November> 

有人可以为我建议一个解决方案吗?如果是这样,我应该让 MySQL 或 PHP 完成大部分工作吗?我正在寻找一个优雅的解决方案,该解决方案还考虑到上面的规则 1 和 2。

我是否应该对表格进行非规范化并存储准时交货状态和日期差异以提高性能?我很抱歉发了这么长的帖子。我还在学习,我会很感激一些建议。

4

0 回答 0