select * from tb order by 指标类型,序号

解决方案 »

  1.   

    楼上有点马虎,没理解楼主意思
    我的思路是这样
    select * from 表 where 指标类型='月度指标' order by 序号
    union all
    select * from 表 where 指标类型='季度指标' order by 序号
    union all
    select * from 表 where 指标类型='年度指标' order by 序号
    但是运行不通过,使用order by关键字就无法union,请高手指点
      

  2.   

    是的,如果按照第一个方法指标类型排序应该是先季度然后年度,最后才是月度。所以不对的。第二种方法也是不对的,union进行表连接是不能用order by的。
    我感觉应该是
    select 指标类型,sum(字段1),sum(字段2) from table group by 指标类型。
    但是好像所有的字段都要加入聚合函数才可以,感觉不太对,所以请教一下。
      

  3.   

    --这样就行了:select * from 表 
    order by charindex(指标类型,'月度指标季度指标年度指标'),序号
      

  4.   

    竞争select 主表.指标类型,A1.字段1,A1.字段2,...
    from (select distinct 指标类型 from 原表) As 主表
    left join (select count(*) As A序号,A1.指标类型,A1.字段1,A1.字段2  
             from (select 指标类型,
                   sum(case 指标类型
                      when 月度指标1 then 字段1数量 else 0 end) As 字段1,
                   sum(case 指标类型
                      when 月度指标2 then 字段1数量 else 0 end) As 字段2
                   .....(类推)
                   from 原表 group by 指标类型) As A1 join A1 As A2 
                   on A1.指标类型<A2.指标类型 group by A1.指标类型,A1.字段1,A1.字段2
                   ) As A on A.指标类型=主表.指标类型
    left join .....(类推)