我正在努力将多个查询合并到一个查询中,所有这些都是从单个视图中检索所有查询的相同日期范围,即
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
FROM view_orders where order_datetime between '2017-10-01 00:00:00' and '2017-10-05 23:59:59' and parent_id IS NULL
AND business_id_fk is not null GROUP BY business_id_fk ORDER BY total DESC;
以上查询是为了明智地计算
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-10-01 00:00:00' and '2017-10-05 23:59:59'
AND parent_id IS NULL
AND business_id_fk is null
ORDER BY total DESC;
这是为了获得具有空值的business_id_fk的计数
select ((SELECT ifnull(count(*),0) as 'total' FROM view_orders where (sla_del_datetime > delivery_datetime) and order_datetime between '2017-10-01 00:00:00' and '2017-10-05 23:59:59' and parent_id is null group by business_id_fk) / (SELECT ifnull(count(*),0) as 'total' FROM view_orders where order_status in('DEL','MCP','CMP') order_datetime between '2017-10-01 00:00:00' and '2017-10-05 23:59:59' and and parent_id is null group by business_id_fk)*100) as 'sla_adherence';
此查询是为了获得每个业务明智和非业务的sla_adherence
select ((SELECT ifnull(count(*),0) as 'total' FROM view_orders where order_status in('DEL','MCP','CMP') order_datetime between '2017-10-01 00:00:00' and '2017-10-05 23:59:59' and and parent_id is null group by business_id_fk) / (SELECT ifnull(count(*),0) as 'total' FROM view_orders where order_datetime between '2017-10-01 00:00:00' and '2017-10-05 23:59:59' and parent_id is null group by business_id_fk)*100) as 'strike_rate';
此查询旨在获取每个业务明智和非业务的strike_rate
我希望结果像
business_id_fk || || BUSINESS_NAME总|| ||粗未分配|| ||递送取消|| ||推迟|| Estimated_Revenue || Generated_Revenue || sla_adherence strike_rate
1 || || ABC 10 || || 1 .. | .. | .. | .. | .. | .. | .. | .. | .. | X.XX | X.XX 。 。 。 null(非商业)|| null || .. | .. |。 。 。 。 。 。 | X.XX | X.XX 所有上述字段和非业务的每个业务明智的行。
提前谢谢。
答案 0 :(得分:0)
第一个查询是多行,第二个查询似乎是单个值。因此,如果这些假设是正确的,您可以尝试交叉连接:
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
, cj.sla_adherence
FROM view_orders
CROSS JOIN (
SELECT (
(SELECT ifnull(count(*),0) FROM view_orders where (sla_del_datetime > delivery_datetime) and parent_id is null )
/
(SELECT ifnull(count(*),0) FROM view_orders where order_status in('DEL','MCP','CMP') and parent_id is null )*100.0
) as 'sla_adherence'
) cj
WHERE order_datetime between '2017-08-01 00:00:00' and '2017-10-01 23:59:59'
GROUP BY business_id_fk, business_name, cj.sla_adherence
ORDER BY total DESC
;