First two rows per combination of two columns

Mike V

Given a table like this in PostgreSQL:

Messages

message_id | creating_user_id | receiving_user_id | created_utc
-----------+------------------+-------------------+-------------
 1         | 1                | 2                 | 1424816011
 2         | 3                | 2                 | 1424816012
 3         | 3                | 2                 | 1424816013
 4         | 1                | 3                 | 1424816014
 5         | 1                | 3                 | 1424816015
 6         | 2                | 1                 | 1424816016
 7         | 2                | 1                 | 1424816017
 8         | 1                | 2                 | 1424816018

I want to get the newest two rows per creating_user_id/receiving_user_id where the other user_id is 1. So the result of the query should look like:

message_id | creating_user_id | receiving_user_id | created_utc
-----------+------------------+-------------------+-------------
 1         | 1                | 2                 | 1424816011
 4         | 1                | 3                 | 1424816014
 5         | 1                | 3                 | 1424816015
 6         | 2                | 1                 | 1424816016

Using a window function with row_number() I can get the first 2 messages for each creating_user_id or the first 2 messages for each receiving_user_id, but I'm not sure how to get the first two messages for per creating_user_id/receiving_user_id.

Erwin Brandstetter

Since you filter rows where one of both columns is 1 (and irrelevant), and 1 happens to be the smallest number of all, you can simply use GREATEST(creating_user_id, receiving_user_id) to distill the relevant number to PARTITION BY. (Else you could employ CASE.)

The rest is standard procedure: calculate a row number in a subquery and select the first two in the outer query:

SELECT message_id, creating_user_id, receiving_user_id, created_utc
FROM (
   SELECT *
        , row_number() OVER (PARTITION BY GREATEST (creating_user_id
                                                  , receiving_user_id)
                             ORDER BY created_utc) AS rn
   FROM   messages
   WHERE  1 IN (creating_user_id, receiving_user_id)
   ) sub
WHERE  rn < 3
ORDER  BY created_utc;

Exactly your result.

SQL Fiddle.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Finding all rows with unique combination of two columns

From Dev

Finding all rows with unique combination of two columns

From Dev

Find distinct rows based on combination of two columns

From Dev

Select all rows where two columns contain a combination of values

From Dev

SQL Server Join first two rows as two columns

From Dev

Check if two columns are equal for all rows per group

From Dev

Merge columns with different number of rows based on two first columns in Pandas

From Dev

adding columns to the appropriate rows by matching the first two columns

From Dev

Unique constraint on combination of two columns?

From Dev

Getting distinct combination of two columns

From Dev

Convert two rows to two columns

From Dev

Compare two rows in two columns

From Dev

Two columns in two rows with Bootstrap

From Dev

Appending rows to a .csv file is somehow missing the first two columns of data

From Dev

Datatable : Ordering on two columns and then show only first 10 rows

From Dev

Need to get rows where combination of two columns both exist and don't exit

From Dev

How to select all the SQL rows from a table (With a unique combination of two columns) where the same combination is not already in another table

From Dev

Compare 4 columns in two files; and output the line for unique combination (from first file) and line for duplicate combination (from second file)

From Dev

extract all rows from file_1 except those with first two columns matching the first two columns from file_2

From Dev

postgres sql select combination of two columns

From Dev

Get minimum grouped by unique combination of two columns

From Dev

SQLite Unique Key with a combination of two columns

From Dev

Combination of two columns as unique Key in mysql

From Dev

Mysql - Make combination of two columns unique

From Dev

pandas map one column to the combination of two columns

From Dev

Filter Pandas dataframe based on combination of two columns

From Dev

Searching for value combination in two columns of a database

From Dev

Combination of two columns as unique Key in mysql

From Dev

pairwise combination of string based on two columns

Related Related

  1. 1

    Finding all rows with unique combination of two columns

  2. 2

    Finding all rows with unique combination of two columns

  3. 3

    Find distinct rows based on combination of two columns

  4. 4

    Select all rows where two columns contain a combination of values

  5. 5

    SQL Server Join first two rows as two columns

  6. 6

    Check if two columns are equal for all rows per group

  7. 7

    Merge columns with different number of rows based on two first columns in Pandas

  8. 8

    adding columns to the appropriate rows by matching the first two columns

  9. 9

    Unique constraint on combination of two columns?

  10. 10

    Getting distinct combination of two columns

  11. 11

    Convert two rows to two columns

  12. 12

    Compare two rows in two columns

  13. 13

    Two columns in two rows with Bootstrap

  14. 14

    Appending rows to a .csv file is somehow missing the first two columns of data

  15. 15

    Datatable : Ordering on two columns and then show only first 10 rows

  16. 16

    Need to get rows where combination of two columns both exist and don't exit

  17. 17

    How to select all the SQL rows from a table (With a unique combination of two columns) where the same combination is not already in another table

  18. 18

    Compare 4 columns in two files; and output the line for unique combination (from first file) and line for duplicate combination (from second file)

  19. 19

    extract all rows from file_1 except those with first two columns matching the first two columns from file_2

  20. 20

    postgres sql select combination of two columns

  21. 21

    Get minimum grouped by unique combination of two columns

  22. 22

    SQLite Unique Key with a combination of two columns

  23. 23

    Combination of two columns as unique Key in mysql

  24. 24

    Mysql - Make combination of two columns unique

  25. 25

    pandas map one column to the combination of two columns

  26. 26

    Filter Pandas dataframe based on combination of two columns

  27. 27

    Searching for value combination in two columns of a database

  28. 28

    Combination of two columns as unique Key in mysql

  29. 29

    pairwise combination of string based on two columns

HotTag

Archive