2016-09-20 39 views
-1

My Data in the tables不同的值

Simplification

現在我想在表A的名稱記錄具有不同的值表B.

例如

* A will have records A1,A1,C1,D1 
* B will have records B1,B1,B1,B1 
* C will have records C1,C1 
* D will have records D1,E1 
* E will have only one record E1 

我需要輸出像

A A1,A1,C1,D1 
D D1,E1 

我用ListAgg函數,但它給我所有的記錄。

任何人都可以請告訴我如何獲取記錄

+0

請參閱附加圖片瞭解更多詳情 – anudeep

+2

http://meta.stackoverflow.com/questions/285551/why-may-i-not-upload-images-of-code-on-so-when-asking- a問題/ 285557#285557 –

+0

請輸入您的模式和sql – EoinS

回答

1

嗯。 。 。您不想獲得listagg()的唯一值。你只是想篩選出導致所有的值相同的行:

select a.name, listagg(b.value, ',') within group (order by a.id2) as val_str 
from a join 
    b 
    on a.id2 = b.id2 
group by a.name 
having min(b.value) <> max(b.value); 
+0

謝謝戈登。它幫助我解決了我的問題。 – anudeep

-1

您可以使用連接來連接兩個表上ID2場。 然後您可以在'名稱'上使用select by group by子句和group_concat(在oracle中它是list agg)函數從B表中選擇串聯的值串。 ....如果你正在使用mysql查詢。