我有一个查询
Select Id,DeviceId,TankCount,Tank1_Level,Tank2_Level,ReadTime from Table
Id TankCount DeviceId Tank1_Level Tank2_Level ReadTime
1 1 123 20 50 2014-11-07 14:39:33.277
2 2 456 52 78 2014-11-07 14:39:33.277
3 1 789 44 50 2014-11-07 14:39:33.277
Tank2_Level在所有TankCount = 1行中为50。
当值等于50时,我不想显示Tank2_Level。
TankCount int,Tank1_Level int,Tank2_Level int。
Id TankCount DeviceId Tank1_Level Tank2_Level ReadTime
1 1 123 20 null or empty 2014-11-07 14:39:33.277
2 2 456 52 78 2014-11-07 14:39:33.277
3 1 789 44 null or empty 2014-11-07 14:39:33.277
答案 0 :(得分:1)
SELECT Id,
DeviceId,
TankCount,
Tank1_Level,
CASE WHEN Tank2_Level = '50' and TankCount != '2'
THEN NULL
ELSE Tank2_Level
END AS [Tank2_Level],
ReadTime
FROM table
答案 1 :(得分:1)
Tank2_Level
是数字还是字符串?我假设它是一个数字:
select <other columns>,
(case when Tank2_Level = 50
then 'null or empty'
else cast(Tank2_Level as varchar(255))
end) as Tank2_Level
from . . .;
答案 2 :(得分:1)
SELECT Id,
DeviceId,
TankCount,
Tank1_Level,
CASE WHEN TankCount = 1 AND Tank2_Level = '50' THEN NULL ELSE Tank2_Level END AS Tank2_Level,
ReadTime
FROM Table
答案 3 :(得分:1)
SELECT Id
,DeviceId
,TankCount
,Tank1_Level
,(CASE Tank2_Level WHEN 50 THEN NULL
ELSE Tank2_Level END)
,ReadTime
FROM TABLE
答案 4 :(得分:1)
您可以使用IIF来简化它
Select
Id,
DeviceId,
TankCount,
Tank1_Level,
iif((Tank2_Level=50 and TankCount=1),null,Tank2_Level) as Tank2_Level,
ReadTime
from Table
答案 5 :(得分:1)
SELECT Tank2_Level = CASE WHEN TankCount >= 2 THEN Tank2_Level END
FROM Table