PostgreSQL:有序结果 [英] postgresql: ordered result

查看:109
本文介绍了PostgreSQL:有序结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表格,如下所示:

I've a table that looks like:

 id | user_id | activity_id | activity_type | root_id | is_root | timestamp 
----+---------+-------------+---------------+---------+---------+-----------
  1 |       1 |           1 | text          |       1 |       1 |         5
  2 |       2 |           2 | text          |       1 |       0 |         6
  3 |       3 |           3 | text          |       1 |       0 |        10
  4 |       2 |          10 | text          |      10 |       1 |        50
  5 |       1 |          11 | text          |      10 |       0 |        90
  6 |       3 |          12 | text          |      10 |       0 |       100
  7 |       3 |          20 | text          |      20 |       1 |       190
  8 |       2 |          21 | text          |      20 |       0 |       130
  9 |       3 |          22 | text          |      20 |       0 |       150
 10 |       3 |          22 | text          |      20 |       0 |       150
 11 |       3 |          22 | text          |      20 |       0 |       150

我想要一个像这样的输出

I want an out put like

 id | user_id | activity_id | activity_type | root_id | is_root | timestamp 
----+---------+-------------+---------------+---------+---------+-----------
  7 |       3 |          20 | text          |      20 |       1 |       120
  8 |       2 |          21 | text          |      20 |       0 |       130
 11 |       3 |          22 | text          |      20 |       0 |       150
  9 |       3 |          22 | text          |      20 |       0 |       150
 10 |       3 |          22 | text          |      20 |       0 |       150
  4 |       2 |          10 | text          |      10 |       1 |        50
  5 |       1 |          11 | text          |      10 |       0 |        90
  6 |       3 |          12 | text          |      10 |       0 |       100
  1 |       1 |           1 | text          |       1 |       1 |         5
  2 |       2 |           2 | text          |       1 |       0 |         6
  3 |       3 |           3 | text          |       1 |       0 |        10

root_id应该放在一个组中,并且该组的第一行应该有is_root = 1.应根据根DESC的时间戳对组进行排序,但应将根的子级按ASC排序(基于时间戳)

The root_id should be placed in one group, and the first row of that group should have is_root = 1. The groups should be sorted on the basis of timestamp of root DESC, but the children of the root should be sorted ASC (timestamp based)

该问题的相关列是root_id,is_root,时间戳

感谢您的帮助。

谢谢

推荐答案

您在说这个吗?

ORDER BY root_id DESC, is_root DESC, timestamp

这篇关于PostgreSQL:有序结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆