SQL: filter a joined table

user856358

I have two tables that have a one-many relationship, and I would like to put together a query that follows a rule to join a particular row in the 'many' table to a row in the 'one' table.

user table:

╔════╦══════════════╦
║ id ║  name        ║
╠════╬══════════════╬
║  1 ║ user 1       ║ 
║  2 ║ user 2       ║
║  3 ║ user 3       ║
║  4 ║ user 4       ║
╚════╩══════════════╩

Messages table:

╔════╦══════════════╦═══════════╦═════════╗
║ id ║  Text        ║ user_id   ║   date  ║
╠════╬══════════════╬═══════════╬═════════╣
║  1 ║ Hello        ║    1      ║  3/31   ║
║  2 ║ World        ║    1      ║  4/1    ║
║  3 ║ Test message ║    2      ║  4/2    ║
║  4 ║ Another test ║    3      ║  4/4    ║
╚════╩══════════════╩═══════════╩═════════╝

I am trying to perform a single join from user to messages to get the most recent message for the user. user 2 would have 'test message', user 3 would have 'another test'. User 1 is the one I cannot figure out - I would like to have one row for user 1 returned 'world', based on the fact that it has the most recent date, but I do not see a join that has the capability to perform filtering on a joined table.

Egor Lyah

Try something like this:

SELECT
    message_id
    , [user_id]
    , name
    , [Text]
    , [date]
FROM
(
SELECT
    M.id AS message_id
    , U.id AS [user_id]
    , name
    , [Text]
    , [date]
    --Rank rows for each users by date
    , RANK() OVER(PARTITION BY M.[user_id] ORDER BY [date] DESC, M.id DESC) AS Rnk
FROM
    @messages AS M
    INNER JOIN
    @users AS U
        ON M.[user_id] = U.id
) AS Tmp
WHERE
    --The latest date
    Tmp.Rnk  = 1

This code work in SQL Server 2012 and newer.

This code work in SQL Server 2012 and newer.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

SQL: filter a joined table

From Dev

SQL filter query on joined column

From Dev

Teradata SQL Tuning Multiple columns in a huge table being joined to the same table with OR condition on the filter

From Dev

SQL Row Concatenation on joined table

From Dev

SQL Aggregation of text on joined table

From Dev

sql - how to group by joined table

From Dev

Group results of joined table in sql

From Dev

SQLAlchemy join a "one to many table" and then filter on the joined table with a set of values

From Dev

MySQL return Null = 0 on joined table with filter on one table

From Dev

SQL: Finding totals from a joined table

From Java

Sql update table with joined sums of other tables

From Dev

Complex SQL query - sum of field in joined table

From Dev

SQL inner joining to left joined table

From Dev

SQL Join Constraint Column on Another Joined Table

From Dev

sql query join with condition on joined table

From Dev

SQL: Finding totals from a joined table

From Dev

Add additional joins to already joined table in SQL

From Dev

SQL How to get all rows with a joined table

From Dev

Doctrine 2 - how to SQL ORDER joined table to another table

From Dev

Searching in SQL per POST-Variable in joined Table

From Dev

SQL Server 2012 Max Date in Subquery in a joined table

From Java

Returning one result per id in SQL in a joined table

From Java

SQL How to select from multiple values in joined table

From Dev

Oracle SQL - How do I update from an Outer Joined Table?

From Dev

doctrin2 zf2 native sql joined table

From Dev

SQL Server : splitting a column to multiple columns with joined table

From Dev

SQl sorting based on certain property value of joined table

From Dev

SQL trying to use a column from a joined table in a subquery

From Dev

How to rename a column which contains count function in a joined table in sql?

Related Related

  1. 1

    SQL: filter a joined table

  2. 2

    SQL filter query on joined column

  3. 3

    Teradata SQL Tuning Multiple columns in a huge table being joined to the same table with OR condition on the filter

  4. 4

    SQL Row Concatenation on joined table

  5. 5

    SQL Aggregation of text on joined table

  6. 6

    sql - how to group by joined table

  7. 7

    Group results of joined table in sql

  8. 8

    SQLAlchemy join a "one to many table" and then filter on the joined table with a set of values

  9. 9

    MySQL return Null = 0 on joined table with filter on one table

  10. 10

    SQL: Finding totals from a joined table

  11. 11

    Sql update table with joined sums of other tables

  12. 12

    Complex SQL query - sum of field in joined table

  13. 13

    SQL inner joining to left joined table

  14. 14

    SQL Join Constraint Column on Another Joined Table

  15. 15

    sql query join with condition on joined table

  16. 16

    SQL: Finding totals from a joined table

  17. 17

    Add additional joins to already joined table in SQL

  18. 18

    SQL How to get all rows with a joined table

  19. 19

    Doctrine 2 - how to SQL ORDER joined table to another table

  20. 20

    Searching in SQL per POST-Variable in joined Table

  21. 21

    SQL Server 2012 Max Date in Subquery in a joined table

  22. 22

    Returning one result per id in SQL in a joined table

  23. 23

    SQL How to select from multiple values in joined table

  24. 24

    Oracle SQL - How do I update from an Outer Joined Table?

  25. 25

    doctrin2 zf2 native sql joined table

  26. 26

    SQL Server : splitting a column to multiple columns with joined table

  27. 27

    SQl sorting based on certain property value of joined table

  28. 28

    SQL trying to use a column from a joined table in a subquery

  29. 29

    How to rename a column which contains count function in a joined table in sql?

HotTag

Archive