1
我嘗試從用戶表及其購買的相關產品中創建非定向邊緣列表。要做到這一點,我正在做一個基於產品的桌子上的自我加入,以捕獲通過他們購買相同產品的事實來連接的用戶。我在下面的查詢中遇到的問題是,當我只需要其中一個時,它會返回兩個彼此相反的記錄。這如何實現?我嘗試了一些分析函數,並能夠得到我想要的結果,但是想知道是否有更好的方法,因爲這將運行在約6.5億條記錄上。從自選中創建邊緣列表
create table user_products as (
select 'user1' usr, 'bananas' products from dual
union
select 'user2', 'bananas' from dual
union
select 'user3', 'pickles' from dual
);
select t1.usr, t2.usr
from user_products t1
inner join user_products t2
on t1.products = t2.products
where t1.usr != t2.usr;
查詢結果:
| USR | USR |
|-------|-------|
| user2 | user1 |
| user1 | user2 |
期望的結果:
| USR | USR |
|-------|-------|
| user1 | user2 |
通過LAST_VALUE函數工作,但不知道是否有更好的辦法?
select * from (
select usr userA, last_value(usr ignore nulls) over(partition by products order by products
rows between unbounded preceding and 1 preceding) userB from user_products)
where userB is not null;
查詢結果:
| USERA | USERB |
|-------|-------|
| user2 | user1 |
哈!那一個在我面前是正確的......很高興有第二組眼睛 – Jared