MySQL中的多个聚合函数具有不同的条件

时间:2010-12-12 07:20:41

标签: mysql join group-by

我正在运行以下查询

SELECT t2.lender_name, COUNT(t1.id) as total,    
SUM(t1.submit_date IS NULL) AS num_incomplete,

(SELECT AVG(DATEDIFF(due_date,now())) 
  FROM table_1 WHERE submit_date IS NULL ) as avg_incomplete_due_in,
(SELECT AVG(DATEDIFF(due_date,submit_date))
  FROM table_1 WHERE submit_date IS NOT NULL) as avg_complete_turnaround

FROM table_1
INNER JOIN table_2 t2 ON t2.fid = t1.id
WHERE t1.due_date <= '2010-12-31'
GROUP BY t2.lender_name

总计,num_incomplete和分组效果很好。子选择值对于每一行是相同的。我希望这些值由lender_name分组,并作为同一记录集的一部分返回。有什么建议?

2 个答案:

答案 0 :(得分:3)

您当前的代码只缺少外部查询和子查询之间的关系。理论上,您只需要关联查询:

SELECT t2.lender_name, COUNT(t1.id) as total,
SUM(t1.submit_date IS NULL) AS num_incomplete,
 (SELECT AVG(DATEDIFF(due_date,now())) 
  FROM table_1 t3
  WHERE submit_date IS NULL
    AND t3.lender_name = t2.lender_name) as avg_incomplete_due_in,
 (SELECT AVG(DATEDIFF(due_date,submit_date))
  FROM table_1
  WHERE submit_date IS NOT NULL
    AND t3.lender_name = t2.lender_name) as avg_complete_turnaround
FROM table_1 t1
INNER JOIN table_2 t2 ON t2.fid = t1.id
WHERE t1.due_date <= '2010-12-31'
GROUP BY t2.lender_name

实际上,MySQL中的查询效率不高。您可以通过以下方式重写它:

SELECT
  t2.lender_name,
  COUNT(*) as total,
  SUM(t1.submit_date IS NULL) AS num_incomplete,
  AVG(IF(t1.submit_date IS NULL,
         DATEDIFF(t1.due_date, NOW()),
         NULL)) AS avg_incomplete_due_in,
  AVG(DATEDIFF(due_date,submit_date)) AS avg_complete_turnaround
FROM table_1 t1
INNER JOIN table_2 t2 ON t2.fid = t1.id
WHERE t1.due_date <= '2010-12-31'
GROUP BY t2.lender_name

答案 1 :(得分:0)

总结是否,并计算是否做了伎俩

SELECT t2.lender_name, COUNT(t1.id) as total,    
SUM(t1.submit_date IS NULL) AS num_incomplete,
SUM(IF(table_1.submit_date IS NULL,DATEDIFF(table_1.due_date,now()),0)) / COUNT(IF(table_1.submit_date IS NULL,1,NULL)) as avg_incomplete_due_in
SUM(IF(table_1.submit_date IS NOT NULL,DATEDIFF(table_1.due_date,submit_date),0)) / COUNT(IF(table_1.submit_date IS NOT NULL,1,NULL)) as avg_complete_turnaround

FROM table_1
INNER JOIN table_2 t2 ON t2.fid = t1.id
WHERE t1.due_date <= '2010-12-31'
GROUP BY t2.lender_name