开发者

SELECT sales by user pairs

I have a Session table

    UserId    SessionId
    1         a
    2         a
    1         b
    4         b
    2         c
    3         c

And another sales table 开发者_开发知识库where the sales are tied to a SessionID. I would like to get the sales by unique pairs of users from the Session table without having to use a cursor.


SELECT  ss1.userid, ss2.userid, SUM(sales)
FROM    session ss1
JOIN    session ss2
ON      ss2.sessionid = ss1.sessionid
        AND ss2.userid < ss1.userid
JOIN    sales s
ON      s.sessionid = ss1.sessionid
GROUP BY
        ss1.userid, ss2.userid
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜