通过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板块 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   英镑请问各位高手有没有什么好办法?谢谢

解决方案 »

  1.   

    order by t.bk,t.company ,t.code
      

  2.   


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

  3.   

    加一个这样的条件
    decode(t.name,'库存现金',0,'人民币现金',2,'美元现金',3,'人民币',4,......)
      

  4.   

    order by t.company ,t.code 
      

  5.   

    try: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 
      

  6.   

    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           活期存款
    */
      

  7.   

    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 
      

  8.   

    order by t.bk,t.company ,t.code