使用COUNT()从select语句中计算百分比 [英] To calculate using the COUNT() a percentage from a select statement
问题描述
亲爱的专家,
需要使用COUNT()计算整数和条件COUNT()
如下图所示。
姓名年龄
------ -------
amy 6
pam 8
joe 10
pet 16 >
jon 2
ana 12
单个Select语句计算百分比使用
COUNT()函数的10岁以上儿童在MS SQL SERVER 2008中使用
谢谢
我的尝试:
我能够单独计算它们,但我需要一个选择语句
Dear Expert,
Need to calculate using the COUNT() for the Whole and for a conditional COUNT()
as depicted below.
name age
-------------
amy 6
pam 8
joe 10
pet 16
jon 2
ana 12
A single Select statement to calculate the percentage of children who are above 10 using the
COUNT() function
in MS SQL SERVER 2008
Thanks
What I have tried:
I am able to calculate them separately, but I need One select statement
推荐答案
尝试
SELECT COUNT(*) AS Total, SUM(CASE WHEN age > 10 THEN 1 ELSE 0 END) * 100 / COUNT(*) AS Percent FROM MyTable
Select Count (*) * 100 / (Select Count(*) From YourTable) as Score
From YourTable where age>10
运行副本: SQL小提琴 [ ^ ]
请不要直接复制并粘贴sql。
Running Copy:SQL Fiddle[^]
Please don't directly copy and paste the sql.
这篇关于使用COUNT()从select语句中计算百分比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!