我无法创建有效的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。
我应该对表格进行非规范化并按时交付状态和日期差异以提高性能吗?对不起,我很抱歉。我还在学习,我希望得到一些建议。