【问题标题】:Aggregating a unique pair of column values from the same table based on a common column value基于公共列值从同一个表中聚合一对唯一的列值
【发布时间】:2014-07-26 00:42:16
【问题描述】:

我有下表:

my_table
------------------------
| common_id | uniq_val |
------------------------
|    1      |    foo   |          
------------------------
|    1      |    bar   |          
------------------------

我想从中聚合值,使得结果查询看起来像:

DESIRED RESULT
---------------------------------------
| common_id | uniq_val_1 | uniq_val_2 |
---------------------------------------
|    1      |     foo    |      bar   |
---------------------------------------

OR

---------------------------------------
| common_id | uniq_val_1 | uniq_val_2 |
---------------------------------------
|    1      |     bar    |      foo   |
---------------------------------------

所以我写了查询:

SELECT t1.common_id, t1.uniq_val, t2.uniq_val
FROM my_table t1 JOIN my_table AS t2 
ON t1.common_id=t2.common_id 
WHERE t1.uniq_val!=t2.uniq_val;

结果

RESULTING SELECT
---------------------------------------
| common_id | uniq_val_1 | uniq_val_2 |
---------------------------------------
|    1      |     foo    |      foo   |
---------------------------------------
|    1      |     bar    |      bar   |
---------------------------------------

但我只需要其中一列,所以我应该能够执行 GROUP BY t1.common_id,例如:

SELECT t1.common_id, t1.uniq_val, t2.uniq_val
FROM my_table t1 JOIN my_table AS t2 
ON t1.common_id=t2.common_id 
WHERE t1.uniq_val!=t2.uniq_val
GROUP BY t1.common_id;

不幸的是,这会返回错误:

ERROR:  column "t1.uniq_val" must appear in the GROUP BY clause or be used in an aggregate function

谁能指出我的逻辑错误?

【问题讨论】:

  • 每个 common_id 值是否总是只有 2 个唯一值?有的有 2 个,有的有 3 个或 4 个吗?

标签: sql postgresql join group-by


【解决方案1】:

简单聚合怎么样?

select common_id, min(uniq_val) as uniq_val_1, max(uniq_val) as uniq_val_2
from my_table
group by common_id;

【讨论】:

    【解决方案2】:

    你可以试试distinct on

    SELECT distinct on (t1.common_id) t1.common_id, t1.uniq_val, t2.uniq_val FROM my_table t1 JOIN my_table AS t2 ON t1.common_id=t2.common_id WHERE t1.uniq_val!=t2.uniq_val;

    我认为它会产生你需要的东西!

    【讨论】:

    • 给出错误:在“,”或附近出现语法错误,你确定这适用于 postgres 吗?
    • 我刚刚使用了您的查询,您说的那个有效并添加了distinct on (t1.common_id)。请注意,此字段与您选择的第一个字段 (t1.common_id) 之间没有逗号!
    【解决方案3】:

    这将处理每个 common_id 最多 10 个 uniq_val 值。如果需要,您可以删除或添加更少或更多的 uniq_val 值。

    在此处查看具有不同计数的 uniq_val 值的 common_id 值的演示: http://sqlfiddle.com/#!15/e2c87/1/0

    select common_id,
           max(case when rn = 1 then uniq_val else null end) as uniq_val_1,
           max(case when rn = 2 then uniq_val else null end) as uniq_val_2,
           max(case when rn = 3 then uniq_val else null end) as uniq_val_3,
           max(case when rn = 4 then uniq_val else null end) as uniq_val_4,
           max(case when rn = 5 then uniq_val else null end) as uniq_val_5,
           max(case when rn = 6 then uniq_val else null end) as uniq_val_6,
           max(case when rn = 7 then uniq_val else null end) as uniq_val_7,
           max(case when rn = 8 then uniq_val else null end) as uniq_val_8,
           max(case when rn = 9 then uniq_val else null end) as uniq_val_9,
           max(case when rn = 10 then uniq_val else null end) as uniq_val_10
    from(
    select row_number() over (partition by common_id order by common_id, uniq_val) as rn,
           common_id,
           uniq_val
      from my_table
     order by common_id, uniq_val) x
     group by common_id
     order by common_id
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-02
      • 2020-04-13
      • 2018-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多