我有下表
session_id | page_viewed
1 | A
1 | B
1 | C
2 | B
2 | E
我想做的是将page_viewed列与其自身进行交叉联接,但是在session_id的分区上进行交叉联接。因此,从上面的表中查询将返回:
session_id | page_1 | page_2
1 | A | A
1 | A | B
1 | A | C
1 | B | A
1 | B | B
1 | B | C
1 | C | A
1 | C | B
1 | C | C
2 | B | B
2 | B | E
2 | E | B
2 | E | E
我今天研究了窗口函数,试图找到解决方法,但是似乎无法使用join函数。有人可以帮忙吗?
答案 0 :(得分:2)
嗯。 。 。您似乎想要自我加入:
select t1.session_id, t1.page_viewed as page_1, t2.page_viewed as page_2
from t t1 join
t t2
on t1.session_id = t2.session_id
order by t1.session_id, t1.page_viewed, t2.page_viewed;
答案 1 :(得分:2)
您可以加入session_id
作为加入条件:
SELECT
t1.session_id,
t1.page_viewed AS page_1,
t2.page_viewed AS page_2
FROM yourTable t1
INNER JOIN yourTable t2
ON t1.session_id = t2.session_id;
-- ORDER BY clause optional, if you need it here