2013-03-25 101 views
1

如果我有兩個表 - Table_ATable_B - 如果我使用LEFT JOIN加入他們,我怎麼纔可以過濾來自Table_B與在Table_A行不止一次參加這些行?
DB味:Teradata的LEFT JOIN過濾

+0

你的意思是GROUP BY? – 2013-03-25 08:59:17

+0

GROUP BY和HAVING count()> 1 – kufudo 2013-03-25 09:01:04

+0

對不起,我必須糾正這個問題 - 我必須從參與連接的Table_B中過濾所有的行不止一次 – Adam 2013-03-25 09:01:32

回答

1

如果我沒有記錯的Teradata支持窗口的功能,所以這可能會奏效:

select * 
from (
    select a.*, 
     b.* 
     count(*) over (partition by a.MyCol) as cnt 
    from Table_A a 
    left join Table_B b ON a.MyCol = b.MyCol 
    where ... -- Conditions 
) t 
where cnt > 1 

(未測試)

+0

完美!而已!謝謝! – Adam 2013-03-25 09:47:18

0

可能是幫助你

1) you can used INNER JOIN . 
2) you can also check joind row is not null or blank . 
0
Select a.*,b.* from Table_A a 
left join Table_B b on condition 
HAVING COUNT(DISTINCT a.value)>1 

作出必要的修改和檢查

+0

不工作..... – Adam 2013-03-25 09:34:49

1

這是你接受的答案的一個特定的Teradata版本:

select a.*, 
     b.* 
from Table_A a 
left join Table_B b 
ON a.MyCol = b.MyCol 
where ... -- Conditions 
QUALIFY count(*) over (partition by a.MyCol) > 1 

注意QUALIFY是Teradata數據擴展ANSI標準(而且是一個方便的一個)。