Sqlserver的case when 用法
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Sqlserver的case when 用法相关的知识,希望对你有一定的参考价值。
select PayAmount from Table where ID=123,怎么用case when求PayAmount
---下文举例分析了case when常用的用法,如下所示:涉及排序字段的应用create table test
(
qty int ,
sort varchar(20)
)
insert into test(qty,sort)values
(1,'a'),(2,'b'),(3,'d'),(1,'e')
go
----方法1:
select sort,qty,
case qty
when 1 then '少'
when 2 then '中'
when 3 then '多'
else '未知'
end as [数量范围]
from test
--方法2:
select sort,qty,
case
when qty=1 then '少'
when qty=2 then '中'
when qty=3 then '多'
else '未知'
end as [数量范围]
from test
---sum统计用法
select
sum( case when qty=1 then 1 else 0 end) as [少],
sum( case qty when 2 then 1 else 0 end) as [中],
sum( case when qty=3 then 1 else 0 end) as [多],
sum( case when qty<> 1 and qty <>2 and qty <>3 then 1 else 0 end) as [位置]
from test
---case when 做排序字段
declare @i int
set @i=0
select * from test
order by
case @i when 0 then qty else sort end
go
truncate table test
drop table test追问
哇 !
参考技术A 你直接取出来就好了啊,你要附加什么条件?case when 的用法给你举个例子
select case when payamount>100 then 100 else payamount end from table where id=123
如果payamount大于100 ,则取100,其余取原值。追问
select sum(PayAmount) from Table where ID=123,要在聚合函数中使用,所以sum(case when....then....else...end),括号里面的内容不会写
2015.7.17 case when then else end用法Oralcle与SQLserver一致
SELECT
CASE airway_point_type_id
WHEN 1 THEN ‘VOR‘
WHEN 2 THEN ‘VOR/DME‘
WHEN 3 THEN ‘NDB‘
WHEN 10 THEN ‘P字点‘
WHEN 11 THEN ‘五字代码点‘
ELSE ‘其它‘ END
航路点类型
FROM AIRWAY_POINT
GROUP BY AIRWAY_POINT_TYPE_ID
以上是关于Sqlserver的case when 用法的主要内容,如果未能解决你的问题,请参考以下文章