sql

dvtswwa3  于 2021-07-24  发布在  Java
关注(0)|答案(1)|浏览(250)

我有一张这样的table:

---------------------------------------------------------
| ArticleID |ReleaseTime           | PurchaseTime       |
--------------------------------------------------------
|       7   |  7/24/20 3:00 PM     |   NULL             |
--------------------------------------------------------
|       5   |  7/16/20 1:00 PM     |   NULL             | 
---------------------------------------------------------
|       4   |  7/24/20 2:00 PM     |   NULL             |
--------------------------------------------------------
|       1   |         NULL         |   7/25/20 5:45 PM  | 
--------------------------------------------------------
|       3   |         NULL         |   7/26/20 9:00 AM  |
--------------------------------------------------------
|       3   |     7/25/20 8:30 AM  |    NULL            | 
---------------------------------------------------------
|       1   |     7/24/20 5:00 PM  |    NULL            |
--------------------------------------------------------
|       1   |         NULL         |   7/25/20 6:00 PM  | 
---------------------------------------------------------
|       6   |     7/24/20 3:30 PM  |    NULL            |

它需要按releasetime asc排序,然后按该id的所有purchasetime asc排序。结果应如下所示:

---------------------------------------------------------
| ArticleID |ReleaseTime           | PurchaseTime       |
--------------------------------------------------------
|       5   |  7/16/20 1:00 PM     |   NULL             |
--------------------------------------------------------
|       4   |  7/24/20 2:00 PM     |   NULL             | 
---------------------------------------------------------
|       7   |  7/24/20 3:00 PM     |   NULL             |
--------------------------------------------------------
|       6   |  7/24/20 3:30 PM     |   NULL             | 
--------------------------------------------------------
|       1   |  7/24/20 5:00 PM     |   NULL             |
--------------------------------------------------------
|       1   |         NULL         |   7/25/20 5:45 PM  |
--------------------------------------------------------
|       1   |         NULL         |   7/25/20 6:00 PM  |
--------------------------------------------------------
|       3   |  7/25/20 8:30 AM     |   NULL             | 
---------------------------------------------------------
|       3   |         NULL         |   7/26/20 9:00 AM  |

有什么建议吗?提前谢谢!

t98cgbkg

t98cgbkg1#

如果我理解正确的话,您希望按每个文件的最短发布时间进行排序 id 然后按购买时间排序。
可以在中使用窗口函数 order by :

order by min(releasetime) over (partition by id),
         id,
         purchasetime asc

相关问题