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