通过执行下列SQL排序后: 
select t.bk      "板块", 
      t.company "公司", 
      t.code    "科目编码", 
      t.name    "科目名称" 
  from tbl_test t 
  where t.bk='xx板块' 
order by t.code,t.company 得到的结果这样的:
xx板块 A公司 1001 库存现金
xx板块 B公司 1001 库存现金
xx板块 C公司 1001 库存现金
xx板块 A公司 100101   人民币现金
xx板块 C公司 100101   人民币
xx板块 A公司 100102   美元现金
xx板块 C公司 100102   美元
xx板块 C公司 100103   欧元
xx板块 C公司 100104   英镑
xx板块 A公司 1002 银行存款
xx板块 B公司 1002 银行存款
xx板块 C公司 1002 银行存款
xx板块 A公司 100201   人民币活期存款
xx板块 B公司 100201   中信富华支行
xx板块 C公司 100201   活期存款
而我想要的结果是这样的: xx板块 B公司 1001 库存现金
xx板块 A公司 1001 库存现金
xx板块 A公司 100101   人民币现金
xx板块 A公司 100102   美元现金
xx板块 C公司 1001 库存现金
xx板块 C公司 100101   人民币
xx板块 C公司 100102   美元
xx板块 C公司 100103   欧元
xx板块 C公司 100104   英镑
xx板块 B公司 1002 银行存款
xx板块 B公司 100201   中信富华支行
xx板块 A公司 1002 银行存款
xx板块 A公司 100201   人民币活期存款请问各位高手有没有什么好办法?谢谢

解决方案 »

  1.   

    你说一下你想如何排列,看你想要的结果是先按板块,然后按公司,然后按科目,最后按科目名称来排
    order by t.bk,t.company,t.code,t.name
    这里的t.code要按编号顺序走,t.name是按拼音,笔划排吗?
    适当的在order by 加decode(t.name,.....)可以实现你的要求。
      

  2.   

    try:[code=Assembly]select 
          t.bk      "板块", 
          t.company "公司", 
          t.code    "科目编码", 
          t.name    "科目名称" 
    from 
          tbl_test t 
    where 
          t.bk='xx板块' 
    order by 
          substr(t.code,4),t.company,t.company,t.code [/code]
      

  3.   

    select 
          t.bk      "板块", 
          t.company "公司", 
          t.code    "科目编码", 
          t.name    "科目名称" 
    from 
          tbl_test t 
    where 
          t.bk='xx板块' 
    order by 
          substr(t.code,4),t.company,t.company,t.code [/code]
      

  4.   

    SQL Server 下的测试结果:
    declare @t table(code1 varchar(16),code2 varchar(16),code3 varchar(16),code4 varchar(16))insert into @t select 'xx板块','A公司','1001'  ,'库存现金'
    insert into @t select 'xx板块','B公司','1001'  ,'库存现金'
    insert into @t select 'xx板块','C公司','1001'  ,'库存现金'
    insert into @t select 'xx板块','A公司','100101','人民币现金' 
    insert into @t select 'xx板块','C公司','100101','人民币'
    insert into @t select 'xx板块','A公司','100102','美元现金' 
    insert into @t select 'xx板块','C公司','100102','美元'
    insert into @t select 'xx板块','C公司','100103','欧元'
    insert into @t select 'xx板块','C公司','100104','英镑'
    insert into @t select 'xx板块','A公司','1002'  ,'银行存款'
    insert into @t select 'xx板块','B公司','1002'  ,'银行存款'
    insert into @t select 'xx板块','C公司','1002'  ,'银行存款'
    insert into @t select 'xx板块','A公司','100201','人民币活期存款' 
    insert into @t select 'xx板块','B公司','100201','中信富华支行' 
    insert into @t select 'xx板块','C公司','100201','活期存款' select * from @t order by left(code3,4),code2,code3,code4/*
    code1            code2            code3            code4            
    ---------------- ---------------- ---------------- ---------------- 
    xx板块             A公司              1001             库存现金
    xx板块             A公司              100101           人民币现金
    xx板块             A公司              100102           美元现金
    xx板块             B公司              1001             库存现金
    xx板块             C公司              1001             库存现金
    xx板块             C公司              100101           人民币
    xx板块             C公司              100102           美元
    xx板块             C公司              100103           欧元
    xx板块             C公司              100104           英镑
    xx板块             A公司              1002             银行存款
    xx板块             A公司              100201           人民币活期存款
    xx板块             B公司              1002             银行存款
    xx板块             B公司              100201           中信富华支行
    xx板块             C公司              1002             银行存款
    xx板块             C公司              100201           活期存款
    */
      

  5.   

    select t.bk      "板块", 
          t.company "公司", 
          t.code    "科目编码", 
          t.name    "科目名称" 
      from tbl_test t 
      where t.bk='xx板块' 
    order by t.bk,substr(t.code,1,4),t.company,t.code;
      

  6.   

    是这样的:先要按编码排序,然后,要将公司相同的科目放在一起,比如:
    B公司 1001
    A公司 1001
    A公司 100101
    A公司 100102因为100101、100102都是1001的下级,所以它们要归在一起,而不能是
    A公司 1001
    B公司 1001
    A公司 100101
    A公司 100102不知道各位能否理解我的意思?谢谢!
      

  7.   

    select t.bk      "板块", 
          t.company "公司", 
          t.code    "科目编码", 
          t.name    "科目名称" 
      from tbl_test t 
      where t.bk='xx板块' 
    order by substr(t.code,1,4),t.company,t.code;
      

  8.   


    我在3楼的那个SQL也不能得出楼主的预期?
      

  9.   

    Sorry,之前多写了个t.company:select 
          t.bk      "板块", 
          t.company "公司", 
          t.code    "科目编码", 
          t.name    "科目名称" 
    from 
          tbl_test t 
    where 
          t.bk='xx板块' 
    order by 
          substr(t.code,4),t.company,t.code 
      

  10.   

    select t.bk      "板块", 
          t.company "公司", 
          t.code    "科目编码", 
          t.name    "科目名称" 
      from tbl_test t 
      where t.bk='xx板块' 
    order by substr(t.code,4),t.company
      

  11.   

    select t.bk      "板块", 
          t.company "公司", 
          t.code    "科目编码", 
          t.name    "科目名称" 
      from tbl_test t 
      where t.bk='xx板块' 
    order by substr(t.code,4),t.code,t.company
      

  12.   

    哦,不好意思lz,我没看到where条件,order by后t.bk是多余的,9楼和11楼的答案是正确的
      

  13.   

    order by 
          substr(t.code,1,4),t.company,t.code 就实现了,不过 libin_ftsafe 能给解释一下原理吗?谢谢
      

  14.   

    取t.code的前四位进行排序,然后按公司排序,然后按科目排序