表字段如下:
ID  名字    年份    重要贡献事件想用一条SQL比较两个不同的人,在某一年的“重要贡献事件”的对比。如,我想查询在2011年,甲乙丙三人对公司做出重要贡献的事件的数量,最终想得到的结果如下:
甲    乙    丙
10    15    13
请问SQL语句如何书写?谢谢

解决方案 »

  1.   

    select 
    sum(case when 名字='甲' then 1 else 0 end) as 甲,
    sum(case when 名字='乙' then 1 else 0 end) as 乙,
    sum(case when 名字='丙' then 1 else 0 end) as 丙
    from tb where 年份=2011
      

  2.   


    select
    sum(case when 名字='甲' then 1 else 0 end) as [甲],
    sum(case when 名字='乙' then 1 else 0 end) as [乙],
    sum(case when 名字='丙' then 1 else 0 end) as [丙]
    from tb
    where year='2011'
      

  3.   

    year改成年份
    如果重要贡献事件不是数字的话如上