Self-join of a subquery

前端 未结 3 1260
慢半拍i
慢半拍i 2021-02-04 02:53

I was wondering, is it possible to join the result of a query with itself, using PostgreSQL?

3条回答
  •  野性不改
    2021-02-04 03:04

    Do you mean, the result of a query on a table, to that same table. If so, then Yes, it's possible... e.g.

    --Bit of a contrived example but...
    SELECT * 
    FROM Table
    INNER JOIN
    (
        SELECT 
              UserID, Max(Login) as LastLogin
        FROM
              Table
        WHERE 
              UserGroup = 'SomeGroup'
        GROUP BY
              UserID
    
    ) foo
    ON Table.UserID = Foo.UserID AND Table.Login = Foo.LastLogin
    

提交回复
热议问题