-1

我有一个选择命令的结果集:

1 Null Null Null
1 1    Null Null
1 1    Null 1
1 1    1    Null

但我需要像这样重新排序:

1 Null Null Null
1 1    Null Null
1 1    1    Null
1 1    Null 1

我怎样才能做到这一点?

选择语句:

 select K,
        Null As M,
        Null As T1,
        Null As T2,
        SUM(debit) SumDebit 
   from h 
        join i On h.HdrId = i.HdrIdRef 
        Group by K 

           Union 

 select K,
        M,
        Null As T1,
        Null As T2,
        SUM(debit) SumDebit 
    from h 
         join i On h.HdrId = i.HdrIdRef 
         Group by K,M 

             Union 
 Select * 
   From
       ( select K,
                M,
                T1,
                Null As T2,
                SUM(debit) SumDebit 
           from h 
                join i On h.HdrId = i.HdrIdRef 
                Group by K,M,T1
      ) A 
  Where A.T1 Is Not Null 

              Union 

 Select * 
  From
     ( select K,
              M,
              T1,
              T2,
              SUM(debit) SumDebit 
          from h 
               join i On h.HdrId = i.HdrIdRef 
               Group by K,M,T1,T2
     ) A 
 Where A.T2 Is Not Null 
       Order By K, M, T1, T2 
4

1 回答 1

1
SELECT *
FROM table
ORDER BY ISNULL(Col4, 0), ISNULL(Col3, 0), ISNULL(Col2, 0), ISNULL(Col1, 0)

我假设您希望根据您的输出从最右边的列开始排序

于 2013-07-20T05:00:13.577 回答