我有一个SELECT DISTINCT...
查询,它产生以下输出:
id | name | _pref_ | _num_
----------------+----------------+----------------+-------
Cf-1 | Cf-1 | Cf- | 1
Cf-2 | Cf-2 | Cf- | 2
Cf-3 | Cf-3 | Cf- | 3
Cf-5 | Cf-5 | Cf- | 5
Me-1 | Me-1 | Me- | 1
Me-2 | Me-2 | Me- | 2
Me-3 | Me-3 | Me- | 3
Me-4 | Me-4 | Me- | 4
Me-5 | Me-5 | Me- | 5
Me-6 | Me-6 | Me- | 6
Me-7 | Me-7 | Me- | 7
Me-8 | Me-8 | Me- | 8
Me-9 | Me-9 | Me- | 9
Me-10 | Me-10 | Me- | 10
Me-11 | Me-11 | Me- | 11
Me-12 | Me-12 | Me- | 12
Me-13 | Me-13 | Me- | 13
Me-14 | Me-14 | Me- | 14
Me-15 | Me-15 | Me- | 15
Me-16 | Me-16 | Me- | 16
Me-18 | Me-18 | Me- | 18
Me-20 | Me-20 | Me- | 20
Me-22 | Me-22 | Me- | 22
Me-24 | Me-24 | Me- | 24
RC-1 | RC-1 | RC- | 1
RC-2 | RC-2 | RC- | 2
RM | RM | RM |
Ronda Hospital | Ronda Hospital | Ronda Hospital |
(28 rows)
_ pref _和_ num _只是对[[name列的计算,使我从用户的角度更直观地对行进行排序。
但是他们没有添加其他信息,所以我将从输出中将其删除。
问题是,当我尝试这样做时,出现以下错误:joanmi@alpha:~/.../SQL/gis$ node layer.carreteres_menorca.sql.js list | pg geogps
ERROR: para SELECT DISTINCT, las expresiones en ORDER BY deben aparecer en la lista de resultados
LINE 43: order by _pref_, _num_, nom
我知道我可以再次将其包装在另一个CTE中,或将其用作子查询,然后按列排序,但在我看来,这不是正确的解决方案...我确定应该有某种方式告诉Postgres这些列依赖于其他列,因此即使使用DISTINCT子句也可以将它们从输出中省略。
产生先前输出的查询是这个:
WITH layer as ( select computed.name as id , computed.name || '-' || id as part_id , computed.name as name , label , name as codiTram , ST_AsEWKT(geom) as geom , regexp_replace( name , '^([^0-9]+).*$' , '\1' , 'i' ) as _pref_ , nullif( regexp_replace( name , '^[^0-9]*([0-9]+)?.*$' , '\1' , 'i' ) , '')::integer as _num_ from "Carreteres_Menorca" , lateral ( select regexp_replace( name , '^.*?Me[-.]*([0-9]+).*$' , 'Me-\1' , 'i' ) as name ) as computed where name is not null ) select distinct id, name, _pref_, _num_ from layer order by _pref_, _num_, name
group by id, name
,您应该获得想要的结果:select id, name
from layer
group by id, name
order by max(_pref_), max(_num_), name