如何在SQL中正确使用GROUP BY语句?

时间:2019-03-27 06:19:50

标签: mysql sql

我需要对销售线索数据库运行查询,该查询需要细分商店名称,该商店的总销售线索以及转换后的总销售线索。到目前为止,以下是我所得到的:

SELECT  stores.store_name as Store, COUNT(*) as "Total Lead", 
        (SELECT COUNT(*) as "Converted Lead" FROM leads WHERE lead_status_id = 5) 
FROM "leads" 
INNER JOIN "employee_leads" ON "employee_leads"."lead_id" = "leads"."id" 
INNER JOIN "employees" ON "employees"."id" = "employee_leads"."employee_id" 
INNER JOIN "emp_stores" ON "emp_stores"."employee_id" = "employees"."id" 
INNER JOIN "stores" ON "stores"."id" = "emp_stores"."store_id" 
GROUP BY store_name;

但是,这是错误的,因为转换后的零件计算的是所有转换后的潜在客户,而不是某个商店。我得到的结果如下:

 +------------+-------------+----------------+
 | Store      | Total Lead  | Converted Lead |
 +------------+-------------+----------------+
 | Store1     |         10  |            10  |
 | Store2     |         14  |            10  |
 | Store3     |         17  |            10  |
 +------------+-------------+----------------+

当前在“已转换线索”列中列出了所有商店的总已转换线索。对于商店1,“已转换线索”列假定为4,对于商店2为3,对于商店3为4。我尝试进行sub-groupby,但是,由于每个商店将有1行以上,因此出现错误。我需要查询来计算每个商店的转换线索,如下所示:

 +------------+-------------+----------------+
 | Store      | Total Lead  | Converted Lead |
 +------------+-------------+----------------+
 | Store1     |         10  |             4  |
 | Store2     |         14  |             3  |
 | Store3     |         17  |             4  |
 +------------+-------------+----------------+

有人可以指出我正确的方向吗?谢谢

3 个答案:

答案 0 :(得分:2)

您可以尝试使用条件汇总函数代替子查询。

SELECT  stores.store_name as Store, COUNT(*) as "Total Lead", 
        COUNT(CASE WHEN lead_status_id = 5 THEN 1 END) "Converted Lead" 
FROM "leads" 
INNER JOIN "employee_leads" ON "employee_leads"."lead_id" = "leads"."id" 
INNER JOIN "employees" ON "employees"."id" = "employee_leads"."employee_id" 
INNER JOIN "emp_stores" ON "emp_stores"."employee_id" = "employees"."id" 
INNER JOIN "stores" ON "stores"."id" = "emp_stores"."store_id" 
GROUP BY store_name;

如果您使用的是mysql,则可以尝试使用COUNT(lead_status_id = 5)

SELECT  stores.store_name as Store, COUNT(*) as "Total Lead", 
        COUNT(lead_status_id = 5) "Converted Lead" 
FROM "leads" 
INNER JOIN "employee_leads" ON "employee_leads"."lead_id" = "leads"."id" 
INNER JOIN "employees" ON "employees"."id" = "employee_leads"."employee_id" 
INNER JOIN "emp_stores" ON "emp_stores"."employee_id" = "employees"."id" 
INNER JOIN "stores" ON "stores"."id" = "emp_stores"."store_id" 
GROUP BY store_name;

答案 1 :(得分:2)

您没有将子查询与商店名称相关联。尽管可以在其where子句中完成此操作,但更简单的方法是对case表达式进行计数,以仅对具有相关状态的销售线索进行计数:

SELECT  stores.store_name as Store, 
        COUNT(*) as "Total Lead", 
        COUNT(CASE lead_status_id WHEN 5 THEN 1 END) AS "Converted Lead"
FROM "leads" 
INNER JOIN "employee_leads" ON "employee_leads"."lead_id" = "leads"."id" 
INNER JOIN "employees" ON "employees"."id" = "employee_leads"."employee_id" 
INNER JOIN "emp_stores" ON "emp_stores"."employee_id" = "employees"."id" 
INNER JOIN "stores" ON "stores"."id" = "emp_stores"."store_id" 
GROUP BY store_name;

答案 2 :(得分:0)

请尝试这个。

SELECT  stores.store_name as Store, COUNT(*) as "Total Lead", 
        (SELECT COUNT(*) as "Converted Lead" FROM leads AS SS WHERE SS.id =A.id AND lead_status_id = 5) 
FROM "leads" AS A 
INNER JOIN "employee_leads" ON "employee_leads"."lead_id" = "A"."id" 
INNER JOIN "employees" ON "employees"."id" = "employee_leads"."employee_id" 
INNER JOIN "emp_stores" ON "emp_stores"."employee_id" = "employees"."id" 
INNER JOIN "stores" ON "stores"."id" = "emp_stores"."store_id" 
GROUP BY store_name;