日期:2014-05-18  浏览次数:20438 次

求一个case when语句。急!!谢谢
declare @cs int
set @cs9=(select gongzuoxuanxiang from MoNiLiangShuRuShangZai where dtuid=@StationName and lushu=1)
把查询出来的值赋给@cs这个变量,如果这个变量的值是1的话,那么Demo10这个字段列名显示为加压泵1电流,如果这个变量值是2,Demo1这个字段列名显示为加压泵2电流,如果是3就显示为加压泵3电流,依次类推。这个SQL语句如何写呀?Demo10这个字段是另一个表中的字段。

下面的语句是我写的,但是总提示有错误。',' 附近有语法错误。


select distinct c.StationName as 站名,Testtime as 召测时间 ,Demo10=case @cs9 when '1'then'加压泵1电流' when '2'then '加压泵2电流' when '3' then '加压泵3电流' when '4' then '加压泵4电流' when '5' then '加压泵5电流' when '6' then '加压泵6电流', when '7' then '深井泵1电流' when '8' then '深井泵2电流' when '9' then '深井泵3电流' when '10' then '深井1水位' when '11' then '水池水位' when '13' then '压力返馈',Demo11 as 深井水位,Demo1 as 深水井电流,Demo2 as 深水井电流2,Demo3 as 深水井电流3,Demo4 as 加压泵电流1,Demo5 as 加压泵电流2,Demo6 as 加压泵电流3, Demo7 as 加压泵电流4 ,Demo8 as 加压泵电流5,Demo9 as 加压泵电流6
from History a LEFT OUTER JOIN Stationinfo c ON a.StationName=c.Agreement
where a.StationName=@StationName and Testtime between @stime and @etime 


------解决方案--------------------
select distinct c.StationName as 站名,Testtime as 召测时间 ,Demo10=case @cs9 when '1' then'加压泵1电流' when '2'then '加压泵2电流' when '3' then '加压泵3电流' when '4' then '加压泵4电流' when '5' then '加压泵5电流' when '6' then '加压泵6电流', when '7' then '深井泵1电流' when '8' then '深井泵2电流' when '9' then '深井泵3电流' when '10' then '深井1水位' when '11' then '水池水位' when '13' then '压力返馈' END,Demo11 as 深井水位,Demo1 as 深水井电流,Demo2 as 深水井电流2,Demo3 as 深水井电流3,Demo4 as 加压泵电流1,Demo5 as 加压泵电流2,Demo6 as 加压泵电流3, Demo7 as 加压泵电流4 ,Demo8 as 加压泵电流5,Demo9 as 加压泵电流6
from History a LEFT OUTER JOIN Stationinfo c ON a.StationName=c.Agreement
where a.StationName=@StationName and Testtime between @stime and @etime

------解决方案--------------------
select distinct c.StationName as 站名,Testtime as 召测时间 ,Demo10=case @cs9 when '1'then'加压泵1电流' when '2'then '加压泵2电流' when '3' then '加压泵3电流' when '4' then '加压泵4电流' when '5' then '加压泵5电流' when '6' then '加压泵6电流', when '7' then '深井泵1电流' when '8' then '深井泵2电流' when '9' then '深井泵3电流' when '10' then '深井1水位' when '11' then '水池水位' when '13' then '压力返馈' end,Demo11 as 深井水位,Demo1 as 深水井电流,Demo2 as 深水井电流2,Demo3 as 深水井电流3,Demo4 as 加压泵电流1,Demo5 as 加压泵电流2,Demo6 as 加压泵电流3, Demo7 as 加压泵电流4 ,Demo8 as 加压泵电流5,Demo9 as 加压泵电流6
from History a LEFT OUTER JOIN Stationinfo c ON a.StationName=c.Agreement
where a.StationName=@StationName and Testtime between @stime and @etime 
红色逗号没有,后面的如楼上说加end
------解决方案--------------------
select distinct c.StationName as 站名,Testtime as 召测时间 ,Demo10=case @cs9 when '1'then'加压泵1电流' when '2'then '加压泵2电流' when '3' then '加压泵3电流' when '4' then '加压泵4电流' when '5' then '加压泵5电流' when '6' then '加压泵6电流', when '7' then '深井泵1电流' when '8' then '深井泵2电流' when '9' then '深井泵3电流' when '10' then '深井1水位' when '11' then '水池水位' when '13' then '压力返馈' End,Demo11 as 深井水位,Demo1 as 深水井电流,Demo2 as 深水井电流2,Demo3 as 深水井电流3,Demo4 as 加压泵电流1,Demo5 as 加压泵电流2,Demo6 as 加压泵电流3, Demo7 as 加压泵电流4 ,Demo8 as 加压泵电流5,Demo9 as 加压泵电流6
from History a LEFT OUTER JOIN Stationinfo c ON a.StationName=c.Agreement
where a.StationName=@StationName and Testtime between @stime and @etime


记得Case语法格式为以下两种:
Case <表达式> When <value1> Then <Key1> … When <valueN> Then <KeyN> Else <KeyX> End
Case When <表达式1> Then <Key1> … When <表达式N> Then <KeyN> Else <KeyX> End