我如何在 1 table 中加入不同的筛选区域
How can i join different filtered areas in 1 table
我正在尝试从 table 命名订单中查找物流公司和货运公司的综合表现。我写了一个工作代码,但它运行缓慢,我希望它运行得更快。
工作代码是;
'SELECT
a.*,
b.number_of_order_lost_or_damaged,
c.number_of_order_lost_tracking FROM
(SELECT
logistic_partner,
cargo_partner,
round(AVG(DATEDIFF(date_delivered, date_add)),2) as time_spent_in_delivery
FROM
orders
WHERE
status IN (6)
GROUP BY logistic_partner , cargo_partner) a
JOIN
(SELECT
logistic_partner,
cargo_partner,
COUNT(id) AS number_of_order_lost_or_damaged
FROM
orders
WHERE
status IN (8)
GROUP BY logistic_partner , cargo_partner) b USING (logistic_partner , cargo_partner)
JOIN
(SELECT
logistic_partner,
cargo_partner,
COUNT(id) AS number_of_order_lost_tracking
FROM
orders
WHERE
status IN (10)
GROUP BY logistic_partner , cargo_partner) c USING (logistic_partner , cargo_partner);'
无效代码是
'SELECT
o.logistic_partner,
o.cargo_partner,
round(AVG(DATEDIFF(a.date_delivered, a.date_add)),2) as time_spent_in_delivery,
count(b.id),
count(c.id) FROM orders o
JOIN orders a on a.id=o.id and a.status = 6
JOIN orders b on o.id=b.id and b.status = 8
JOIn orders c on c.id=o.id and c.status = 10
GROUP BY logistic_partner , cargo_partner;'
但它们是分开工作的:
'SELECT
o.logistic_partner,
o.cargo_partner,
round(AVG(DATEDIFF(a.date_delivered, a.date_add)),2) as time_spent_in_delivery FROM orders o
JOIN orders a on a.id=o.id and a.status = 6
GROUP BY logistic_partner , cargo_partner;'
或
SELECT
o.logistic_partner,
o.cargo_partner,
count(b.id) FROM orders o
JOIN orders b on o.id=b.id and b.status = 8
GROUP BY logistic_partner , cargo_partner;
可能是什么问题?
我猜您不需要任何这些派生表。使用条件聚合应该会产生相同的结果。例如:
SELECT logistic_partner
, cargo_partner
, ROUND(AVG(IF(status = 6, DATEDIFF(date_delivered, date_add), NULL)),2) AS time_spent_in_delivery
, SUM( IF( status = 8, 1, 0) ) AS number_of_order_lost_or_damaged
, SUM( IF( status = 10, 1, 0) ) AS number_of_order_lost_tracking
FROM orders
GROUP BY logistic_partner
, cargo_partner
我正在尝试从 table 命名订单中查找物流公司和货运公司的综合表现。我写了一个工作代码,但它运行缓慢,我希望它运行得更快。
工作代码是;
'SELECT
a.*,
b.number_of_order_lost_or_damaged,
c.number_of_order_lost_tracking FROM
(SELECT
logistic_partner,
cargo_partner,
round(AVG(DATEDIFF(date_delivered, date_add)),2) as time_spent_in_delivery
FROM
orders
WHERE
status IN (6)
GROUP BY logistic_partner , cargo_partner) a
JOIN
(SELECT
logistic_partner,
cargo_partner,
COUNT(id) AS number_of_order_lost_or_damaged
FROM
orders
WHERE
status IN (8)
GROUP BY logistic_partner , cargo_partner) b USING (logistic_partner , cargo_partner)
JOIN
(SELECT
logistic_partner,
cargo_partner,
COUNT(id) AS number_of_order_lost_tracking
FROM
orders
WHERE
status IN (10)
GROUP BY logistic_partner , cargo_partner) c USING (logistic_partner , cargo_partner);'
无效代码是
'SELECT
o.logistic_partner,
o.cargo_partner,
round(AVG(DATEDIFF(a.date_delivered, a.date_add)),2) as time_spent_in_delivery,
count(b.id),
count(c.id) FROM orders o
JOIN orders a on a.id=o.id and a.status = 6
JOIN orders b on o.id=b.id and b.status = 8
JOIn orders c on c.id=o.id and c.status = 10
GROUP BY logistic_partner , cargo_partner;'
但它们是分开工作的:
'SELECT
o.logistic_partner,
o.cargo_partner,
round(AVG(DATEDIFF(a.date_delivered, a.date_add)),2) as time_spent_in_delivery FROM orders o
JOIN orders a on a.id=o.id and a.status = 6
GROUP BY logistic_partner , cargo_partner;'
或
SELECT
o.logistic_partner,
o.cargo_partner,
count(b.id) FROM orders o
JOIN orders b on o.id=b.id and b.status = 8
GROUP BY logistic_partner , cargo_partner;
可能是什么问题?
我猜您不需要任何这些派生表。使用条件聚合应该会产生相同的结果。例如:
SELECT logistic_partner
, cargo_partner
, ROUND(AVG(IF(status = 6, DATEDIFF(date_delivered, date_add), NULL)),2) AS time_spent_in_delivery
, SUM( IF( status = 8, 1, 0) ) AS number_of_order_lost_or_damaged
, SUM( IF( status = 10, 1, 0) ) AS number_of_order_lost_tracking
FROM orders
GROUP BY logistic_partner
, cargo_partner