SELECT DISTINCT 省略 ORDER BY 中存在的列
SELECT DISTINCT omitting columns present in ORDER BY
我有一个 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]+).*$'
, ''
, 'i'
) as _pref_
, nullif(
regexp_replace(
name
, '^[^0-9]*([0-9]+)?.*$'
, ''
, 'i'
)
, '')::integer as _num_
from "Carreteres_Menorca"
, lateral (
select regexp_replace(
name
, '^.*?Me[-.]*([0-9]+).*$'
, 'Me-'
, '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
我有一个 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]+).*$'
, ''
, 'i'
) as _pref_
, nullif(
regexp_replace(
name
, '^[^0-9]*([0-9]+)?.*$'
, ''
, 'i'
)
, '')::integer as _num_
from "Carreteres_Menorca"
, lateral (
select regexp_replace(
name
, '^.*?Me[-.]*([0-9]+).*$'
, 'Me-'
, '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