修改查询输出为 Q1Count 、Q2Count、Q3Count 和Q4Count 所占 Q1Count +Q2Count+Q3Count +Q4Count 总数的百分比string query1 = "SELECT COUNT(CASE WHEN 季度 = 'Q1' THEN 1 ELSE NULL END) AS Q1Count, COUNT(CASE WHEN 季度 = 'Q2' THEN 1 ELSE NULL END) AS Q2Count,COUNT(CASE WHEN 季度 = 'Q3' THEN 1 ELSE NULL END) AS Q3Count,COUNT(CASE WHEN 季度 = 'Q4' THEN 1 ELSE NULL END) AS Q4Count FROM FAIsum GROUP BY 季度";
时间: 2024-01-27 16:04:52 浏览: 81
可以在查询语句的最后加上以下代码来计算总数和百分比:
```
, COUNT(*) AS TotalCount,
CONCAT(ROUND(SUM(CASE WHEN 季度 = 'Q1' THEN 1 ELSE 0 END)/COUNT(*)*100,2), '%') AS Q1Percent,
CONCAT(ROUND(SUM(CASE WHEN 季度 = 'Q2' THEN 1 ELSE 0 END)/COUNT(*)*100,2), '%') AS Q2Percent,
CONCAT(ROUND(SUM(CASE WHEN 季度 = 'Q3' THEN 1 ELSE 0 END)/COUNT(*)*100,2), '%') AS Q3Percent,
CONCAT(ROUND(SUM(CASE WHEN 季度 = 'Q4' THEN 1 ELSE 0 END)/COUNT(*)*100,2), '%') AS Q4Percent
```
其中,COUNT(*) AS TotalCount 计算总数,SUM(CASE WHEN 季度 = 'Q1' THEN 1 ELSE 0 END) 计算 Q1Count 的总数,其他季度同理。
最后,使用 CONCAT 函数将计算出的百分比与百分号拼接在一起。
修改后的完整查询语句如下:
```
SELECT
COUNT(CASE WHEN 季度 = 'Q1' THEN 1 ELSE NULL END) AS Q1Count,
COUNT(CASE WHEN 季度 = 'Q2' THEN 1 ELSE NULL END) AS Q2Count,
COUNT(CASE WHEN 季度 = 'Q3' THEN 1 ELSE NULL END) AS Q3Count,
COUNT(CASE WHEN 季度 = 'Q4' THEN 1 ELSE NULL END) AS Q4Count,
COUNT(*) AS TotalCount,
CONCAT(ROUND(SUM(CASE WHEN 季度 = 'Q1' THEN 1 ELSE 0 END)/COUNT(*)*100,2), '%') AS Q1Percent,
CONCAT(ROUND(SUM(CASE WHEN 季度 = 'Q2' THEN 1 ELSE 0 END)/COUNT(*)*100,2), '%') AS Q2Percent,
CONCAT(ROUND(SUM(CASE WHEN 季度 = 'Q3' THEN 1 ELSE 0 END)/COUNT(*)*100,2), '%') AS Q3Percent,
CONCAT(ROUND(SUM(CASE WHEN 季度 = 'Q4' THEN 1 ELSE 0 END)/COUNT(*)*100,2), '%') AS Q4Percent
FROM FAIsum
GROUP BY 季度
```
阅读全文