考虑以下存储过程:
alter procedure getRisks @id int, @convertedRiskValue int
as
select top 1 table1.id, idRisk, riskValue from table1
left join table2
on table1.id = table2.id
where table1.id= @id
order by table2.id desc
idRisk
值始终在1-4之间变化,riskValue
的变化范围为0-1000。
我想做什么?
riskValue
转换为idRisk
“类型”,如下所示:,然后将其放入@convertedRiskValue
并从select语句中返回。
有什么想法吗?
答案 0 :(得分:1)
SELECT
riskValue,
CASE
WHEN riskValue > -1 AND riskValue < 251 THEN 1
WHEN riskValue > 250 AND riskValue < 501 THEN 2
WHEN riskValue > 500 AND riskValue < 751 THEN 3
WHEN riskValue > 750 AND riskValue < 1001 THEN 4
END AS idRisk
FROM Table
答案 1 :(得分:1)
试试这个:
alter procedure getRisks @id int, @convertedRiskValue int
as
select top 1 table1.id, CASE WHEN RiskValue >= 0 AND RiskValue <= 250 THEN 1
WHEN RiskValue >= 251 and RiskValue <= 500 THEN 2
WHEN RiskValue >= 501 and RiskValue <= 750 THEN 3
WHEN RiskValue >= 751 and RiskValue <= 1000 THEN 4 end as idRiskType
, riskValue from table1
left join table2
on table1.id = table2.id
where table1.id= @id
order by table2.id desc