2014-07-26 110 views
0

我有如下表:汇总基于一个共同的列值从同一个表中唯一的一对列值的

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 

任何人都可以指出我的逻辑错误吗?

+0

将分别common_id值始终只有2个独特的价值?有些人有2人,其他人有3人或4人? –

回答

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; 
+0

谢谢,很好地工作 – darko

0

你可以尝试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;

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

+0

给出错误:语法错误在或接近“,”你确定这适用于postgres? – darko

+0

我刚刚使用了你的查询,你说的那个查询正在工作,并在(t1.common_id)'上添加了'distinct。请注意,这与您选择的第一个字段(t1.common_id)之间没有逗号! –

0

这将处理每个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 
相关问题