英文:
I need to calculate the sum correctly for the three calculated columns
问题
I need help with my query, where the total of 3 counted columns in one column as total, here is my current query:
SELECT region, COUNT(s_code), COUNT(b_code), COUNT(d_code), SUM(COUNT(s_code, b_code, d_code)) as total
FROM shilpy
GROUP BY region
ORDER BY region
英文:
I need help with my query, where the total of 3 counted columns in one column as total, here is my current query:
SELECT region, COUNT(s_code), COUNT(b_code), COUNT(d_code), SUM(COUNT(s_code, b_code, d_code)) as total
FROM shilpy
GROUP BY region
ORDER BY region
答案1
得分: 1
如果您只需要总数,您可以在行内直接求和:
SELECT region,
COUNT(s_code) + COUNT(b_code) + COUNT(d_code) AS total
FROM shilpy
GROUP BY region
ORDER BY region
英文:
If you only need the total, you can just sum it up inline:
SELECT region,
COUNT(s_code) + COUNT(b_code) + COUNT(d_code) AS total
FROM shilpy
GROUP BY region
ORDER BY region
答案2
得分: 0
你可以使用子查询来计算特定的计数,然后获取计数的总和,而无需重新扫描整个表格:
select *, (count_s + count_b + count_d) as total
from (
select region, count(s_code) as count_s, count(b_code) as count_b, count(d_code) as count_d
from shilpy
group by region
order by region
) as s;
英文:
You can use a subquery to calculate specific counts, and then get the total of counts without having to rescan the entire table again:
select *, (count_s + count_b + count_d) as total
from (
select region, count(s_code) as count_s, count(b_code) as count_b, count(d_code) as count_d
from shilpy
group by region
order by region
) as s;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论