MySQL: 有没有办法 select 列并在 "multiple ways" 中使用它?
MySQL: Is there a way to select a column and use it in "multiple ways"?
我正在学习 MySQL,但我遇到了一个找不到答案的问题。我有一个包含列的数据库:company 和 company_response。我现在已经按降序查询了这些公司,以便解决最多投诉并放心的公司排在最前面。问题是我现在必须计算“Closed with relief”实例的比率。还有其他两个响应选项,我找不到制作 “比率”列的方法(关闭救济/关闭所有投诉) 以便我可以在救济金额旁边显示比率.有没有人可以帮助我解决这个问题?
在实践中,我不知道如何创建“所有实例”和比率列,因为我已经使用 where 子句来创建我的 Total relieves
列
到目前为止,这是我的查询:
SELECT company AS "Company name", count(company_response) AS "Total relieves"
FROM cfpb_complaints_2500
WHERE company_response = "Closed with relief"
GROUP BY company
ORDER BY count(company_response) DESC;
尝试使用条件计数:
SELECT company AS "Company name",
COUNT(CASE WHEN company_response = 'Closed with relief' THEN 1 END) as "Total Relieves",
COUNT(CASE WHEN company_response = 'Closed with relief' THEN 1 END) / COUNT(*) as "Ratio of Relieves"
FROM cfpb_complaints_2500
GROUP BY company
您可以使用子查询来计算来自公司的所有投诉
CREATE tABLE cfpb_complaints_2500 (company int,company_response varchar(100))
insert INTO cfpb_complaints_2500 VALUES (1,"Closed with relief")
,(1,"Closed with relief")
,(1,"Closed without relief")
,(1,"Closed without relief")
,(1,"Closed without relief")
,(1,"Closed without relief"),(2,"Closed with relief")
,(2,"Closed with relief")
,(2,"Closed without relief")
,(2,"Closed without relief")
SELECT company AS "Company name"
,count(company_response) AS "Total relieves"
, count(company_response)/ (SELECT count(*) FROM cfpb_complaints_2500 WHERE company = cf.company) AS "ratio"
FROM cfpb_complaints_2500 cf
WHERE company_response = "Closed with relief"
GROUP BY company
ORDER BY ratio DESC;
Company name | Total relieves | ratio
-----------: | -------------: | -----:
2 | 2 | 0.5000
1 | 2 | 0.3333
db<>fiddle here
我正在学习 MySQL,但我遇到了一个找不到答案的问题。我有一个包含列的数据库:company 和 company_response。我现在已经按降序查询了这些公司,以便解决最多投诉并放心的公司排在最前面。问题是我现在必须计算“Closed with relief”实例的比率。还有其他两个响应选项,我找不到制作 “比率”列的方法(关闭救济/关闭所有投诉) 以便我可以在救济金额旁边显示比率.有没有人可以帮助我解决这个问题?
在实践中,我不知道如何创建“所有实例”和比率列,因为我已经使用 where 子句来创建我的 Total relieves
列
到目前为止,这是我的查询:
SELECT company AS "Company name", count(company_response) AS "Total relieves"
FROM cfpb_complaints_2500
WHERE company_response = "Closed with relief"
GROUP BY company
ORDER BY count(company_response) DESC;
尝试使用条件计数:
SELECT company AS "Company name",
COUNT(CASE WHEN company_response = 'Closed with relief' THEN 1 END) as "Total Relieves",
COUNT(CASE WHEN company_response = 'Closed with relief' THEN 1 END) / COUNT(*) as "Ratio of Relieves"
FROM cfpb_complaints_2500
GROUP BY company
您可以使用子查询来计算来自公司的所有投诉
CREATE tABLE cfpb_complaints_2500 (company int,company_response varchar(100))
insert INTO cfpb_complaints_2500 VALUES (1,"Closed with relief") ,(1,"Closed with relief") ,(1,"Closed without relief") ,(1,"Closed without relief") ,(1,"Closed without relief") ,(1,"Closed without relief"),(2,"Closed with relief") ,(2,"Closed with relief") ,(2,"Closed without relief") ,(2,"Closed without relief")
SELECT company AS "Company name" ,count(company_response) AS "Total relieves" , count(company_response)/ (SELECT count(*) FROM cfpb_complaints_2500 WHERE company = cf.company) AS "ratio" FROM cfpb_complaints_2500 cf WHERE company_response = "Closed with relief" GROUP BY company ORDER BY ratio DESC;
Company name | Total relieves | ratio -----------: | -------------: | -----: 2 | 2 | 0.5000 1 | 2 | 0.3333
db<>fiddle here