0
聚合查詢聚集的MySQL查詢
1.
SELECT business_id_fk,business_name,
IFNULL(count(orderid_customer),0) AS total,
IFNULL(sum(CASE WHEN order_status IN ('CRD') THEN 1 ELSE 0
END), 0) AS crude,
IFNULL(sum(CASE WHEN order_status IN ('UNA') THEN 1 ELSE 0
END), 0) AS unassigned,
IFNULL(sum(CASE WHEN order_status IN ('DEL' , 'MCP' , 'CMP')
THEN 1 ELSE 0 END), 0) AS delivered,
IFNULL(sum(CASE WHEN order_status IN ('CNL') THEN 1 ELSE 0
END), 0) AS cancelled,
IFNULL(sum(CASE WHEN order_status IN ('CAP' , 'CAD' , 'RSP' ,
'RSD') THEN 1 ELSE 0 END), 0) AS postponed,
IFNULL(SUM(total), 0) AS 'Estimated_Revenue',
IFNULL(SUM(CASE WHEN order_status IN ('DEL' , 'MCP', 'CMP')
THEN total ELSE 0 END), 0) AS 'Generated_Revenue'
FROM view_orders where order_datetime between '2017-08-01
00:00:00' and '2017-10-17 23:59:59' and parent_id IS NULL
GROUP BY business_id_fk ORDER BY total DESC;
2.
SELECT A.business_id_fk,A.business_name,ifnull(((A.TotalPrice/
A.TotalQuantity)*100),0) AS 'sla_adherence' FROM (SELECT
business_id_fk,business_name,
sum(sla_del_datetime > delivery_datetime) AS TotalPrice
,sum(order_status in('DEL','MCP','CMP')) AS TotalQuantity
FROM view_orders where parent_id is null and order_datetime between '2017-
08-01 00:00:00' and '2017-10-17 23:59:59'
GROUP BY business_id_fk) AS A;
3.
SELECT A.business_id_fk,A.business_name,ifnull(((A.Totaldelivered/
A.Totalorders)*100),0) AS 'strike_rate' FROM (SELECT
business_id_fk,business_name,
sum(order_status in('DEL','MCP','CMP')) AS Totaldelivered
, count(*) AS Totalorders
FROM view_orders where parent_id is null and order_datetime between '2017-
08-01 00:00:00' and '2017-10-17 23:59:59'
GROUP BY business_id_fk) AS A;
爲了得到這樣
01結果Business_id_fk BusinessName Total Delivered Unassigned Cancelled Postponed EstimatedRevenue GeneratedRevenue SLAadherance Strikerate
2 DEF 10 5 2 1 0 200 100 100 50
1 ABC 20 10 4 2 0 200 100 100 50
Null Null 10 10 0 0 0 100 100 100 100
這裏有空的商家名稱是單個客戶訂單(非業務訂單)。
預先感謝您:-)
見:我爲什麼要提供什麼,在我看來是一個非常簡單的SQL查詢的MCVE?](https://開頭meta.stackoverflow.com/questions/333952/why-should-i-provide-an-mcve-for-what-seems-to-me-to-be-a-very-simple-sql-query) – Strawberry