[英]How to assign string values to numeric values in a SQL View
Essentially I have a test case scenario where I need to assign calculated percentages to a text value. 本质上,我有一个测试用例场景,我需要将计算出的百分比分配给文本值。 Eg < 80 = 'B' < 70 = 'C' 例如<80 ='B'<70 ='C'
Here is the relevant portion of my code that pulls the value into a percent based on number of assignments handed in. 这是我代码的相关部分,根据上交的分配数量将值拉成百分比。
Select *...
100 * COUNT(CASE TurnedIn WHEN 1 THEN 1 ELSE NULL END)
/ COUNT(TurnedIn) AS Percentage,
From Table1
When ...
I have tried wrapping this with a case such as 我试过用例如
Case when (100 * COUNT(CASE TurnedIn WHEN 1 THEN 1 ELSE NULL END)
/ COUNT(TurnedIn)) <= 80 Then 'B' As LetterGrade
but no luck so far. 但到目前为止没有运气。
You can try this, 你可以试试看
With CTE as
(
Select (100 * COUNT(CASE TurnedIn WHEN 1 THEN 1 ELSE NULL END)
/ COUNT(TurnedIn)) as Percentage
from myTable
)
select Case When Percentage <70
Then 'C'
When Percentage <80
Then 'B'
Else 'A'
End
from CTE
Write as: 写为:
Select *, --name all columns you want
case when Percentage < 80 then 'B'
when Percentage < 70 then 'C'
else 'Add the grade which should be default when above 2
case conditions are not met'
end as LetterGrade
from (
Select *,
100 * COUNT(CASE TurnedIn WHEN 1 THEN 1 ELSE NULL END)
/ COUNT(TurnedIn) AS Percentage
From Table1
When ..) T
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.