次のようなテーブルがあります。
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
みたいなアウトプットが欲しい
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 は 1 つのグループに配置する必要があり、そのグループの最初の行は is_root = 1 にする必要があります。グループはルート DESC のタイムスタンプに基づいて並べ替える必要がありますが、ルートの子は ASC (タイムスタンプ ベース) で並べ替える必要があります。
The relevant columns for the question is root_id, is_root, timestamp
.
どんな助けでも大歓迎です。
ありがとう