sql server案例问题

时间:2011-04-04 08:11:52

标签: sql-server-2005

我的桌子产品有3列 制造商持有价值{'HCL','ACER',HP} model_no PK 键入{PC,打印机,笔记本电脑} 我想在以下列制造商,个人电脑,打印机,笔记本电脑中生成结果集 如果制造商有上述类别的产品,则在各栏中显示“是” 否则没有 即使制造商没有产品,以下代码也会向所有人显示“是” 这是使用while和break语句的要求。请帮助我

select maker,'PC'= 
 case type
when 'pc' then 'Yes' 
when 'printer' then 'Yes'
when 'Laptop' then 'Yes'
else 'No'
end, 
'Laptop'= 
case type
when 'pc' then 'Yes' 
when 'printer' then 'Yes'
when 'Laptop' then 'Yes'
else 'No'
end,'Printer'=
case type
when 'pc' then 'Yes'
when 'printer' then 'Yes'
when 'Laptop' then 'Yes'
else 'No'
end  from product where maker='ACER'

3 个答案:

答案 0 :(得分:1)

select maker,
 case when type IN ('PC','Workstation','Server') THEN 'Yes' ELSE 'No' END AS PC,
 case when type IN ('Laptop','Tablet','Something') THEN 'Yes' ELSE 'No' END AS Laptop,
 case when type IN ('Printer','Plotter','Inkjet') THEN 'Yes' ELSE 'No' END AS Printer

from product 
where maker='ACER'

我冒昧地改变你的逻辑。在每个IN语句中放置有效“是”的条件,然后根据需要添加任意数量。

答案 1 :(得分:0)

select 
  maker,
  case [type] when 'pc'      then 'yes' else 'no' end as PC,
  case [type] when 'Laptop'  then 'yes' else 'no' end as Laptop,
  case [type] when 'Printer' then 'yes' else 'no' end as Printer
from product
where maker = 'ACER'

答案 2 :(得分:-1)

试试这个:

        select maker,
        case type when 'pc' then 'Yes'  when 'printer' then 'Yes' when 'Laptop' then 'Yes'      else 'No' end AS PC, 
case type when 'pc' then 'Yes'  when 'printer' then 'Yes' when 'Laptop' then 'Yes' else 'No' end AS Laptop,
case type when 'pc' then 'Yes' when 'printer' then 'Yes' when 'Laptop' then 'Yes' else 'No' end AS Printer 

from product where maker='ACER' 
相关问题