年份 | 季度 | 数量
1999 | 1    | 1200
1999 | 2    | 1100
1999 | 3    | 1300
1999 | 4    | 1000
2000 | 1    | 1400
2000 | 2    | 900
2000 | 3    | 1250
2000 | 4    | 1000
由这张表查询出如下表:
年份 | 第一季度 | 第二季度 | 第三季度 | 第四季度
1999 | 1200     | 1100     | 1300     | 1000
2000 | 1400     | 900      | 1250     | 1000

解决方案 »

  1.   

    select year, 
    case when season = 1 then sum(count) end,
    case when season = 2 then sum(count) end,
    case when season = 3 then sum(count) end,
    case when season = 4 then sum(count) end
    from t group by year;
      

  2.   

    可能不对,修改下:select year, 
    sum(case when season = 1 then count end),
    sum(case when season = 2 then count end),
    sum(case when season = 3 then count end),
    sum(case when season = 4 then count end)
    from t group by year;