如何根据表中的日期对值进行排序和更新?

时间:2018-04-15 13:19:43

标签: sql

我需要你的帮助。我想在列中对值进行排序,然后保存更改。但我不知道该怎么做。 表看起来像这样:

   Date      |    DocNumber  
-----------------------------    
    2018-15-04 |        1              
    2018-15-07 |        2              
    2018-15-08 |        3              
    2018-15-06 |        4              
    2018-15-05 |        5 

我希望得到类似的东西:

     Date      |    DocNumber  
-----------------------------    
2018-15-04      |    1     
2018-15-05      |    2     
2018-15-06      |    3    
2018-15-07      |    4    
2018-15-08      |    5     

所以我需要对“DocNumber”和“Date”列进行排序。 请帮忙。

1 个答案:

答案 0 :(得分:4)

不建议在每列中单独对值进行排序。但是,它可以做到。这是一种方法:

select max(date) as date, max(docnumber) as docnumber
from ((select row_number() over (order by date) as seqnum, date, NULL as docnumber
       from t
      ) union all
      (select row_number() over (order by docnumber) as seqnum, NULL, docnumber
       from t
      )
     ) dd
group by seqnum
order by seqnum;