我有这样的数据,我想按两个日期排序,我该怎么做

时间:2019-01-10 12:13:08

标签: sql google-bigquery

row_number  date_TY    date_LY   
1          1/6/2019    1/9/2018   
2          1/7/2019    1/10/2018  
3          1/8/2019    1/8/2018  

我的最终输出应如下所示

row_number  date_TY    date_LY  
1          1/6/2019    1/8/2018                          
2          1/7/2019    1/9/2018                       
3          1/8/2019    1/10/2018         

两个日期必须按升序排列

1 个答案:

答案 0 :(得分:0)

这似乎不是一种SQL风格的方法。通常,表中的行代表某些东西,而不是列。

也就是说,您可以使用row_number()和聚合或join来做到这一点:

select max(date_ty) as date_ty, max(date_ly) as date_ly
from ((select date_ty, null as date_ly,
              row_number() over (order by date_ty) as seqnum
       from t
      ) union all
      (select null, date_ly,
              row_number() over (order by date_ly) as seqnum
       from t
      )
     ) t
group by seqnum;

join解决方案是:

select t.date_ty, l.date_ly
from (select t.*,
             row_number() over (order by date_ty) as seqnum
      from t
     ) t join 
     (select t.*,
             row_number() over (order by date_ly) as seqnum
      from t
     ) l
     on t.seqnum = l.seqnum;