id | col1 | col2
---+------+-----
1 | 5 | 1
11| |
8 | 1 | 2
3 | | 1
4 | 1 | (where blanks are nulls)
6 | | 4
2 | 4 | 9
9 | 7 |
10| |
我试图通过COL1降序(最后一个空值)命令,并在的情况下领带(例如,行(8,1,2)和(4,1,)),我想按ID升序排列。
在col1中的其余值为空的情况下,我按col2的降序排序。
所以我得到的表应该是这样的:
id | col1 | col2
---+------+-----
9 | 7 |
1 | 5 | 1
2 | 4 | 9
4 | 1 | (where blanks are nulls)
8 | 1 | 2
6 | | 4
3 | | 1
10| |
11| |
我有我的查询的麻烦
。我已经尝试了做下面的事情,但他们都没有正常工作。
/* This creates the correct ordering, but in the case of ties
they are ignored and don't follow id ascending */
select *
from table
order by
col1 desc nulls last,
col2 desc nulls last,
id asc;
-
/* When this finds a null value, it basically ignores the desc requirement of col 2 */
select *
from table
order by
col1 desc nulls last,
id asc,
col2 desc nulls last;
如果它的事项,我使用PostgreSQL。
任何帮助将不胜感激。谢谢!
谢谢!这工作像一个魅力。我不知道你可以像这样筑巢,学到新东西。 – Raizuri