admin管理员组文章数量:1406924
Having this situation:
SELECT DISTINCT "FieldName"
FROM "TableName"
ORDER BY "FieldName" ASC;
I'd like to have the lines containing '%|%'
first followed by the ones without '%|%'
.
How to achieve this?
Having this situation:
SELECT DISTINCT "FieldName"
FROM "TableName"
ORDER BY "FieldName" ASC;
I'd like to have the lines containing '%|%'
first followed by the ones without '%|%'
.
How to achieve this?
2 Answers
Reset to default 3Use a boolean
expression on the first position of your order by
list. Since true
is 1
, it's larger than a 0
/false
so you need to make that descending
:
demo at db<>fiddle
select*from(select distinct "FieldName"
from "TableName") as subquery
order by "FieldName" like '%|%' desc
, "FieldName" asc;
FieldName |
---|
a|c |
b|d |
aac |
bad |
A select distinct
requires its order by
to use the exact fields that are being selected, so it needs to be nested in a subquery before ordering under a regular select outside.
To avoid it, you can switch to a distinct on
(as suggested by @Erwin Brandstetter), or a plain group by
- after all, it'll extract distinct groups:
select "FieldName"
from "TableName"
group by "FieldName"
order by strpos("FieldName",'|')>0 desc
, "FieldName";
If you're just looking for a |
character, a much more limited but simpler and faster strpos()
should outperform pattern matching with like '%|%'
, similar to '%\|%'
and ~ '\|'
.
With DISTINCT ON
no subquery is required:
SELECT DISTINCT ON (fld, fld ~ '\|')
fld
FROM tbl
ORDER BY fld ~ '\|' DESC, fld;
fiddle
fld ~ '\|'
is effectively the same as fld LIKE '%|%'
.
About DISTINCT ON
:
- Select first row in each GROUP BY group?
About sorting by a boolean
expression:
- Sorting null values after all others, except special
You may want to sort nulls last:
- Sort NULL values to the end of a table
本文标签: sqlSELECTORDER BY ltboolean conditiongtStack Overflow
版权声明:本文标题:sql - SELECT ... ORDER BY <boolean condition> - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744272633a2598258.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
distinct
case. Please don't cross-link RDBMS' - PostgreSQL supports direct boolean expressions inorder by
, SQL Server doesn't, requiring acase
wrapper around them. – Zegarek Commented Mar 24 at 15:30