首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >按以下顺序选择不同的省略列

按以下顺序选择不同的省略列
EN

Stack Overflow用户
提问于 2020-04-27 17:09:00
回答 1查看 47关注 0票数 0

我有一个SELECT DISTINCT...查询,它产生以下输出:

代码语言:javascript
复制
       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列的计算,它允许我从用户的角度以更直观的方式排序行。

但是,他们没有添加额外的信息,,所以我会从输出中删除它们。

问题是,当我尝试这样做时,我会得到以下错误:

代码语言:javascript
复制
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子句也可以从输出中省略它们。

生成前一个输出的查询如下:

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-04-27 17:25:02

如果你group by id, name,你应该得到你想要的结果

代码语言:javascript
复制
select id, name
from layer
group by id, name
order by max(_pref_), max(_num_), name
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61464382

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档