我有一个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发布于 2020-04-27 17:25:02
如果你group by id, name,你应该得到你想要的结果
select id, name
from layer
group by id, name
order by max(_pref_), max(_num_), namehttps://stackoverflow.com/questions/61464382
复制相似问题