2013-08-07 11 views
0

比方說,我有一個表是這樣的:獲取與匹配字段中的所有行的不同列在同一個表

|id|userID|email   |website | 
-------------------------------------- 
|1 |user1 |[email protected]|website.com| 
|2 |user2 |[email protected]|website.com| 
|3 |user3 |[email protected]|website.com| 
|4 |user1 |[email protected]|foo.com | 
|5 |user2 |[email protected]|foo.com | 

,我想所有的行,其中網站='website.com 「並有相應的行與匹配的用戶ID,其中網站=」 foo.com」

所以,在這種情況下它會返回行1和2

任何想法?

回答

1

爲了讓你可以做

select userID 
from your_table 
where website in ('website.com', 'foo.com') 
group by userID 
having count(distinct website) = 2 

,但如果你需要完整的行然後做

select * from your_table 
where userID in 
(
    select userID 
    from your_table 
    where website in ('website.com', 'foo.com') 
    group by userID 
    having count(distinct website) = 2 
) 
+0

這似乎是完美的工作!有什麼辦法可以從匹配的行中獲取所有列而不是僅使用userID? – JGibbers

+0

@JGibel:是的,這是我答案中的第二個查詢。 –

+0

這似乎適用於標準SQL,不幸的是,impala不支持子句中的子查詢。我會發布另一個問題來解決這個問題,謝謝! – JGibbers

2

這裏是單向的用戶:

select t.* 
from t 
where t.website = 'website.com' and 
     exists (select 1 from t t2 where t2.userId = t.userId and t2.website = 'foo.com'); 

編輯:

你也可以表達S作爲聯接:

select distinct t.* 
from t join 
    t2 
    on t2.userId = t.userId and 
     t.website = 'website.com' and 
     t2.website = 'foo.com'; 

如果你知道有沒有重複的,那麼你就可以刪除distinct

+0

據我所知,Hive和Impale都不支持'exists'子句。這是完全有效的sql,但在這種情況下不起作用。 –

1

Hive在使用子查詢(它們在FROM子句中爲are only allowed)方面有點受限,所以我們必須解決這個問題。好消息是,我們並不需要擔心(很多)做大量的連接,因爲Hadoop。 :-)

查找表中匹配行的一種方法是簡單地將表加入到表中。

SELECT left.* 
FROM your_table left 
JOIN your_table right 
ON (left.userID = right.userID) 
WHERE left.website = 'website.com' 
AND right.website = 'foo.com'; 

報告中,我們有相同的表的兩個版本,分別稱爲leftright,我們正在從leftright具有相同的用戶ID(JOIN條件)有一個匹配的行檢索行,但網站是FOO .com(and子句)。

希望有所幫助。

相關問題