Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
351 views
in Technique[技术] by (71.8m points)

sql - 如何在表中找到价值百分比?(How to find percentage of value in table?)

I have a SQL table that contains some of the following data:

(我有一个SQL表,其中包含以下一些数据:)

ID  Name                Grade
54  Brooke  Hammond     Pass
54  Brooke  Hammond     Pass
54  Brooke  Hammond     Fail
82  Carlos  Vaughn      Fail
88  Christina  Rhodes   Fail
88  Christina  Rhodes   Fail
73  Claude  Brady       Fail
86  Clifford  Larson    Fail
78  Darrel  Roy         Pass
75  Darren  Ramos       Pass
59  Delores  Fisher     Fail
50  Elaine  Parsons     Fail
99  Frankie  Figueroa   Fail
96  Geoffrey  Parsons   Fail
96  Geoffrey  Parsons   Fail
96  Geoffrey  Parsons   Pass
84  Gordon  Parsons     Pass
84  Gordon  Parsons     Fail

I'm looking for a SQL statement that will find the percentage of fail scores associated with each person.

(我正在寻找一条SQL语句,该语句将查找与每个人相关的失败分数的百分比。)

The final output I'm looking for is something like Name1: 30%, Name2: 40%, Name3: 20%, etc...

(我正在寻找的最终输出类似于Name1:30%,Name2:40%,Name3:20%等...)

  ask by Ryan Lenz translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In oracle, you can leverage avg aggregate function as following:

(在oracle中,您可以利用avg聚合函数,如下所示:)

 Select id, name, Avg(case when grade = 'Fail' then 1 else 0 end) * 100 as pecntg_failure from your_table Group by id, name; 

Cheers!!

(干杯!!)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...