Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 选择按顺序显示的不同省略列_Sql_Postgresql_Distinct - Fatal编程技术网

Sql 选择按顺序显示的不同省略列

Sql 选择按顺序显示的不同省略列,sql,postgresql,distinct,Sql,Postgresql,Distinct,我有一个selectdistinct…查询,该查询生成以下输出: id | name | _pref_ | _num_ ----------------+----------------+----------------+------- Cf-1 | Cf-1 | Cf- | 1 Cf-2 | Cf-2 | Cf-

我有一个
selectdistinct…
查询,该查询生成以下输出:

       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_uu和num_u只是对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

如果按id、名称分组,则应获得所需的结果:

select id, name
from layer
group by id, name
order by max(_pref_), max(_num_), name

非常简单!!:哈哈:谢谢!我不会的。只是我还没有测试过。对我来说,这似乎真的很简单,但我用智能手机回答了问题,但仍然没有实际测试它。明天我会的。(我是说,不会忘记…)