使用表CARS
:
Make | Model | Color | Year
------- --------- ------- -----
Honda | Accord | Red | 09
Nissan | Skyline | Blue | 15
null | Skyline | Red | 15
Toyota | null | Black | 15
当制作包含值但是模型<时,我试图在制作列中生成字符串错误 / strong>没有。我的陈述目前没有产生任何东西
SELECT
CASE
WHEN (SELECT Count(*)
FROM CARS
WHERE a.make=make
AND a.year=year
AND a.color=color
AND a.model = NULL) AND (SELECT Count(*)
FROM CARS
WHERE a.year=year
AND a.color=color
AND a.make = make) > 1 THEN '**ERROR**'
ELSE a.make
END as make
FROM a.CARS
答案 0 :(得分:1)
当 make 没有任何值时,如果你想错误作为 make 的值,模型然后这可能是你想要的:
SELECT
CASE
WHEN make IS NULL AND model IS NOT NULL
THEN 'ERROR'
ELSE make
END AS make,
model, color, year
FROM cars;
或许我误解了你的意图。您的问题似乎与您在其他答案中发布的评论相同。
答案 1 :(得分:0)
create table cars
(
make varchar(100) null,
model varchar(100) null,
color varchar(100) null,
year int null
);
insert cars(make,model,color,year) values ('honda','accord','red',2009);
insert cars(make,model,color,year) values ('nissan','skyline','blue',2015);
insert cars(make,model,color,year) values (null,'skyline','red',2015);
insert cars(make,model,color,year) values ('toyota',null,'black',2015);
select
(case when c.make is null then 'ERROR' else c.make end) as make,
(case when c.model is null then 'ERROR' else c.model end) as model,
c.color, c.year
from cars c;
修改强>:
select c.make,c.model,c.color,c.year,
case when ( (c.make is null and c.model is not null) or (c.model is null and c.make is not null) ) then 'ERROR' else '' end as col5
from cars c