有没有一种方法可以在SQL中根据一个ID替换结果顺序,同时又根据另一个ID保持分组?

问题描述

我在Postgresql中有此查询

(SELECT q.question,q.category_id,a.id,a.question_id,a.answer
    FROM questions q,answers a
    WHERE q.id = a.question_id
    AND category_id = 1
    AND question_id
    BETWEEN (SELECT property FROM users WHERE email = 'test@test.com')
    AND (SELECT property FROM users WHERE email = 'test@test.com') + 14)
    
    UNION
    
(SELECT q.question,answers a
    WHERE q.id = a.question_id
    AND category_id = 2
    AND question_id
    BETWEEN (SELECT laws FROM users WHERE email = 'test@test.com')
    AND (SELECT laws FROM users WHERE email = 'test@test.com') + 16)
    
ORDER BY question_id,id
当前以以下格式返回结果的

+-----------------------------+-------------+-------------+--------+
|          question           | category_id | question_id | answer |
+-----------------------------+-------------+-------------+--------+
| What color is the sky?      |           1 |          16 | blue   |
| What color is the sky?      |           1 |          16 | green  |
| What color is the sky?      |           1 |          16 | purple |
| What color is the sky?      |           1 |          16 | red    |
| What color is a firetruck?  |           1 |          17 | orange |
| What color is a firetruck?  |           1 |          17 | teal   |
| What color is a firetruck?  |           1 |          17 | red    |
| What color is a firetruck?  |           1 |          17 | green  |
| What color is dirt?         |           2 |          18 | green  |
| What color is dirt?         |           2 |          18 | green  |
| What color is dirt?         |           2 |          18 | green  |
| What color is dirt?         |           2 |          18 | green  |
+-----------------------------+-------------+-------------+--------+

我想做的是根据category_id更改顺序,因此类别id会像这样交替排列:1、2、1,2,但是我想让组基于question_id。因此结果将如下所示:

+-----------------------------+-------------+-------------+--------+
|          question           | category_id | question_id | answer |
+-----------------------------+-------------+-------------+--------+
| What color is the sky?      |           1 |          16 | blue   |
| What color is the sky?      |           1 |          16 | green  |
| What color is the sky?      |           1 |          16 | purple |
| What color is the sky?      |           1 |          16 | red    |
| What color is dirt?         |           2 |          18 | green  |
| What color is dirt?         |           2 |          18 | green  |
| What color is dirt?         |           2 |          18 | green  |
| What color is dirt?         |           2 |          18 | green  |
| What color is a firetruck?  |           1 |          17 | orange |
| What color is a firetruck?  |           1 |          17 | teal   |
| What color is a firetruck?  |           1 |          17 | red    |
| What color is a firetruck?  |           1 |          17 | green  |
+-----------------------------+-------------+-------------+--------+

我尝试使用ORDER BY row_number() OVER (PARTITION BY t.category_id ORDER BY t.category_id) 但这只会导致每个数据都被交替,而没有按question_id分组

解决方法

我认为您想要以下order by子句:

order by 
    rank() over(partition by category_id order by question_id),question_id,id

基本上,这会交错类别/问题元组。

注意:

  • 使用标准的显式联接(from ... join ... on),而不是老式的隐式联接(from ...,... where ...);这是史前语法,不应在新代码中使用

  • 您的查询很可能会简化为不使用union;如果您要提出另一个有关样本数据和所需结果的问题,也许可以提出建议