考虑实用union,在排序字段上慎重些就行了

解决方案 »

  1.   

    select * from 表 where 季度>=1 and 季度<=3
    union all
    select null,null,..sum(数量) 数量...from 表 where 季度>=1 and 季度<=3
    union all
    select * from 表 where 季度>=4 and 季度<=6
    union all
    select null,null,..sum(数量) 数量...from 表 where 季度>=4 and 季度<=6
    .........
      

  2.   


    select date,数量 from
    (
    select convert(varchar(10), 日期,120) date ,数量, 1 as flag 
    from tablename where datepart(quarter,日期)=1union allselect convert(varchar(10), 日期,120),数量,2 
    from tablename where datepart(quarter,日期)=2
    union allselect convert(varchar(10), 日期,120),数量,3
     from tablename where datepart(quarter,日期)=3
    union allselect convert(varchar(10), 日期,120),数量,4
     from tablename where datepart(quarter,日期)=4
    union allselect cast(datepart(quarter,日期) as varchar)+'季度合计' as 合计,sum(数量) 数量, cast (datepart(quarter,日期) as varchar)
    from tablename 
    group by  cast(datepart(quarter,日期) as varchar)+'季度合计',cast (datepart(quarter,日期) as varchar)) aorder by flag