部门  类  工资
   20    1   200
   20    1   200
   20    1   200
   20    2   100
   20    2   100
   30    1   100 想得到结果(两列)
  sum(工资1)sum(工资2)
   600          200

解决方案 »

  1.   

    select sum( case 类 when 1 then 工资 else 0 end) as sum_工资1,
    sum( case 类 when 2 then 工资 else 0 end) as sum_工资2
    from table
      

  2.   

    SELECT
    SUM(CASE WHEN 类 = 1 THEN 工资 ELSE 0 END) AS SUM1,
    SUM(CASE WHEN 类 = 2 THEN 工资 ELSE 0 END) AS SUM2
    FROM 表 GROUP BY 部门
      

  3.   

    select 工资1=sum(case 类 when 1 then 工资 end),工资2=sum(case 类 when 2 then 工资 end) from 工资表 group by 类
      

  4.   

    sum(工资1)sum(工资2)
       600          200应该是700百吧!
      

  5.   


    select sum(case 类 when 1 then 工资 else 0 end) [sum(工资1)],
           sum(case 类 when 2 then 工资 else 0 end)  [sum(工资1)]
    from tb
      

  6.   

    要是600大概  是分组吧select sum( case 类 when 1 then 工资 else 0 end) as sum_工资1,
    sum( case 类 when 2 then 工资 else 0 end) as sum_工资2
    from table
    group by 部门
      

  7.   

    再问下
    我在实际用的时候

    real = SUM(CASE WHEN 部门 LIKE '奖励_%' THEN realJLGZBZ ELSE 0 END)这样用 like 是不是不行