数据表中有如下数据  型号          数量
XABCD-526         7
XABCD-526F       204
XABCD-526F       102
XABCD-526F       102 希望实现
XABCD-526         7
XABCD-526小计     7XABCD-526F       204
XABCD-526F       102
XABCD-526F       102
XABCD-526F小计   408  现在遇上的问题是 对数据进行小计之后,显示时用order by 型号,希望实现上述的功能,可是排序结果变成了
XABCD-526         7
XABCD-526F       204
XABCD-526F       102
XABCD-526F       102
XABCD-526F小计   408
XABCD-526小计     7
是mssql的order by 只对前八位字母排序吗?我如何才能实现我想要的功能呢?
请各位指点一下,谢谢~

解决方案 »

  1.   

    LZ是不是把"小计"包含到型号字段里去了?order by replace(型号,'小计','')试试
      

  2.   

    select 型号,sum(数量),七七八八字段 from 业务表 GROUP by 型号,七七八八字段 
    union all 
    select 型号+‘小计','' sum(数量) from 业务表 group by 型号
    order by 型号
      

  3.   

    order by replace(型号,'小计','')
    这个好```
      

  4.   

    order by 型号,在跟一个字段order by 后面可以跟好几个字段来进行排序``LZ自己看下```
      

  5.   

    select 'XABCD-526' model,7 quantity into #t
    union all select 'XABCD-526F',204
    union all select 'XABCD-526F',102
    union all select 'XABCD-526F',102
    union all select 'XABCD-526',300select model,quantity
    from
    (
    select 
      case when grouping(id)=1 then model+' total' else model end model,
      sum(quantity) quantity,
      id
    from (select *,newid() id from #t) t
    group by model,id with rollup
    ) t
    where model is not null
    order by model/*楼主试试这个,如果你表中有唯一的字段,可用该字段取代newid()*/
      

  6.   

    select 1 id,'XABCD-526' model,7 quantity into #t
    union all select 2,'XABCD-526F',204
    union all select 3,'XABCD-526F',102
    union all select 4,'XABCD-526F',102
    union all select 5,'XABCD-526',300select model,quantity
    from
    (
    select 
      case when grouping(id)=1 then model+' total' else model end model,
      sum(quantity) quantity,
      id
    from #t
    group by model,id with rollup
    ) t
    where model is not null--表中有唯一id的例子
      

  7.   

    if object_id('tempdb.dbo.#tmp')>0
    drop table #tmp 
    create table #tmp([型号] varchar(20),[数量] int)
    insert into #tmp
    select 'XABCD-526',         7
    union all 
    select 'XABCD-526F',       204
    union all 
    select 'XABCD-526F',       102
    union all 
    select 'XABCD-526F',       102 select
    case when (grouping([数量])=1) then isnull([型号]+'合计','合計') else isnull([型号],'unknown') end as [型号],isnull([数量],sum([数量])) as [数量]
    from  #tmp  group by [型号], [数量] with rollup
    /*[型号]            [数量]
    -----------------------------
    XABCD-526    7
    XABCD-526合计    7
    XABCD-526F    102
    XABCD-526F    204
    XABCD-526F合计    408
    合計            415
    */
      

  8.   

    怎么有2条一模一样的数据哦?
    =========================
    希望实现
    XABCD-526         7
    XABCD-526小计     7XABCD-526F       204
    XABCD-526F       102
    XABCD-526F       102
    XABCD-526F小计   408
      

  9.   

    谢谢各位的热心解答
    我采用了gahade(与君共勉) 的方法.