例如,考虑一个具有如下列的表,如何在对每一对的user
属性求和的同时,基于score
中的值来获得不同的2元组组合? >
user | score
-------------
Tom | 13
Sam | 7
Larry | 66
Diana | 29
预期输出:
user1 | user2 | total_score
---------------------------
Tom | Sam | 20
Tom | Larry | 79
Tom | Diana | 42
Sam | Larry | 73
Sam | Diana | 36
Larry | Diana | 95
答案 0 :(得分:1)
我认为您想要一种cross join
:
select t1.name, t2.name, (t1.score + t2.score) as total_score
from t t1 join
t t2
on t1.name > t2.name;