2014-07-26 76 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 
相關問題