Skip to main content
The 2026 Annual Developer Survey is live— take the Survey today!
edited tags
Link
Vao Tsun
http://rextester.com/FFOSS79584
Source Link
Clint T.

PostgreSQL - repeat order by columnconditional ordering

I have the following table:

key | date         | flag
--------------------------
1    now()           true
2    now() - 1 hour  true
3    now() + 1 hour  true
4    now()           false
5    now() - 1 hour  false
6    now() + 1 hour  false

I want the following sorting:

  • First, all rows with flag = false. These rows must be sorted with date asc.
  • Then, all other rows (flag = true). However, these rows must be sorted with date desc.

Is the following query correct?

(
    select *
    from test
    where flag = false
    order by date asc
)
union all
(
    select *
    from test
    where flag = true
    order by date desc
)

Is there a better way to do this? Will the union all keep the rows sorted, and therefore just concatenate the output of the two inner queries?

I don't know how to repeat the columns in the order by, based on a condition.

update

Fiddle can be found here: http://rextester.com/FFOSS79584

PostgreSQL - repeat order by column

I have the following table:

key | date         | flag
--------------------------
1    now()           true
2    now() - 1 hour  true
3    now() + 1 hour  true
4    now()           false
5    now() - 1 hour  false
6    now() + 1 hour  false

I want the following sorting:

  • First, all rows with flag = false. These rows must be sorted with date asc.
  • Then, all other rows (flag = true). However, these rows must be sorted with date desc.

Is the following query correct?

(
    select *
    from test
    where flag = false
    order by date asc
)
union all
(
    select *
    from test
    where flag = true
    order by date desc
)

Is there a better way to do this? Will the union all keep the rows sorted, and therefore just concatenate the output of the two inner queries?

I don't know how to repeat the columns in the order by, based on a condition.

PostgreSQL - conditional ordering

I have the following table:

key | date         | flag
--------------------------
1    now()           true
2    now() - 1 hour  true
3    now() + 1 hour  true
4    now()           false
5    now() - 1 hour  false
6    now() + 1 hour  false

I want the following sorting:

  • First, all rows with flag = false. These rows must be sorted with date asc.
  • Then, all other rows (flag = true). However, these rows must be sorted with date desc.

Is the following query correct?

(
    select *
    from test
    where flag = false
    order by date asc
)
union all
(
    select *
    from test
    where flag = true
    order by date desc
)

Is there a better way to do this? Will the union all keep the rows sorted, and therefore just concatenate the output of the two inner queries?

I don't know how to repeat the columns in the order by, based on a condition.

update

Fiddle can be found here: http://rextester.com/FFOSS79584

Source Link
Clint T.

PostgreSQL - repeat order by column

I have the following table:

key | date         | flag
--------------------------
1    now()           true
2    now() - 1 hour  true
3    now() + 1 hour  true
4    now()           false
5    now() - 1 hour  false
6    now() + 1 hour  false

I want the following sorting:

  • First, all rows with flag = false. These rows must be sorted with date asc.
  • Then, all other rows (flag = true). However, these rows must be sorted with date desc.

Is the following query correct?

(
    select *
    from test
    where flag = false
    order by date asc
)
union all
(
    select *
    from test
    where flag = true
    order by date desc
)

Is there a better way to do this? Will the union all keep the rows sorted, and therefore just concatenate the output of the two inner queries?

I don't know how to repeat the columns in the order by, based on a condition.

lang-sql