select 城市      ,
 sum(case when 货类 = '20' when 1 else 0 end) 货类20,
 sum(case when 货类 = '40' when 1 else 0 end) 货类40,
 sum(case when 货类 = 'HQ' when 1 else 0 end) 货类HQ
from city
group by 城市
order by 城市;

解决方案 »

  1.   

    View 中 出现 case 附近语法错误
      

  2.   

    select 城市      ,
     sum(case when 货类 = '20' then 1 else 0 end) 货类20,
     sum(case when 货类 = '40' then 1 else 0 end) 货类40,
     sum(case when 货类 = 'HQ' then 1 else 0 end) 货类HQ
    from city
    group by 城市
    order by 城市;
      

  3.   

    sorry,change it:select 城市      ,
     sum(case when 货类 = '20' then 1 else 0 end) 货类20,
     sum(case when 货类 = '40' then 1 else 0 end) 货类40,
     sum(case when 货类 = 'HQ' then 1 else 0 end) 货类HQ
    from city
    group by 城市
    order by 城市;
      

  4.   

    呵呵~~~先保存你的视图,可以只是一个select * from aaaa然后双击视图,再把你的SQL语句拷贝进去,确定就行了。
      

  5.   

    第一个回复的答案有笔误,实在抱歉
    第一句语句中的每行的第二个when应该改成then
      

  6.   

    select 城市      ,
     (select count(城市) where 货类 = '40') 货类20,
     (select count(城市) where 货类 = '20') 货类40,
      (select count(城市) where 货类 = ''HQ'') 货类HQ
    from city
    group by 城市
    order by 城市
      

  7.   

    select 城市      ,
     (select count(城市) from city where 货类 = '40') 货类20,
     (select count(城市) from city where 货类 = '20') 货类40,
      (select count(城市) from city where 货类 = ''HQ'') 货类HQ
    from city
    group by 城市
    order by 城市
      

  8.   

    在view 视图中要求 top 加入  否则不行......................................? 如果我加入 top xxx , 可能会出现统计不正确... 怎么办...?
      

  9.   

    select top xxx 城市,
     (select count(城市) from city c1 where 货类 = '40' and c1.城市=c.城市) 货类20,
     (select count(城市) from city c2 where 货类 = '20' and c2.城市=c.城市) 货类40,
      (select count(城市) from city c3 where 货类 = ''HQ'' and c3.城市=c.城市) 货类HQ
    from city c