select single row from multiple rows by id

bill

I have a database table like the following.

  id |  value  |flag
   2    null     1
   2     z       2
   3     x       1
   3     y       2
   3     z       3
   4     null    2

I need result like the following

   id |  value  |flag
   2     z       2
   3     x       1
   4     null     2

If the value is null, the value from other matched id should be taken (2-z-2). If all the rows of an id are not null then the value should be taken by priority which is 1 here (3-x-1). If an id has only null value, take any single row with null value and flag does not matter here.

Julien Vavasseur

This query will take the value of the first not null value (min flag value) or the null row otherwise.

You data:

DECLARE @table TABLE
    ( id INT , val CHAR(1) , flag INT );
INSERT  INTO @table
        ( id, val, flag )
VALUES  ( 2, NULL, 1 )
,       ( 2, 'z', 2 )
,       ( 3, 'x', 1 )
,       ( 3, 'y', 2 )
,       ( 3, 'z', 3 )
,       ( 4, NULL, 2 ); -- NULL only value

Query:

SELECT  id ,
        val ,
        flag
FROM    ( SELECT    * ,
            n = ROW_NUMBER() OVER ( PARTITION BY id ORDER BY CASE
                                                    WHEN val IS NULL
                                                    THEN 0
                                                    ELSE 1
                                                    END DESC, flag )
          FROM      @table AS t
        ) AS o
WHERE   n = 1;

ROW_NUMBER() partitions by id and for each id, it orders by 0 or 1 (null/not null) and flag. The case is used to order by not null values first (=1) and then by null values (=0).

Output:

id  val     flag
2   z       2
3   x       1
4   NULL    1

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to select a single row where multiple rows exist from a table

From Dev

Insert multiple rows from a single row inside an insert into select query

From Dev

How to select a single row where multiple rows exist from a table

From Dev

Combining data from multiple MySql tables, in multiple rows, into a single row for a SELECT statement

From Dev

Aggregate data from multiple rows into single row

From Dev

Select from multiple rows as one row with defaults

From Dev

Get row with the lowest id from multiple rows?

From Dev

SELECT, format rows with different columns into a single row that share an ID

From Dev

How to select multiple rows of mysql table as a single row

From Dev

Convert the multiple rows Select statement result into a single row string result

From Dev

Select multiple rows into a single row one after the other in Postgres

From Dev

SQLite select query to get one row from multiple rows have same id

From Dev

combine data of multiple rows from multiple tables and show in single row

From Dev

MySQL; Make a single row if multiple rows have the same id

From Dev

Select multiple rows from table based on ID

From Dev

single ID value from multiple rows with ID's

From Dev

How to create multiple rows from single row with time interval in MySQL

From Dev

Vectorized implementation to create multiple rows from a single row in pandas dataframe

From Dev

SQL Server - Display columns from multiple rows as single row

From Dev

How to get columns from multiple rows in a single row in SQL

From Dev

How to update a single row from multiple rows with UPDATE JOIN

From Dev

SQL Server - Display columns from multiple rows as single row

From Dev

How to combine values from multiple rows into a single row in Excel?

From Dev

How to combine values from multiple rows into a single row with a comma separator

From Dev

How to combine values from multiple rows into a single row using module?

From Dev

Attempting to aggregate information from multiple rows into a single row

From Dev

Creating multiple rows from single row on specified conditions in hive

From Dev

How to get multiple rows from single row of table?

From Dev

Postgres Multiple Rows as Single Row

Related Related

  1. 1

    How to select a single row where multiple rows exist from a table

  2. 2

    Insert multiple rows from a single row inside an insert into select query

  3. 3

    How to select a single row where multiple rows exist from a table

  4. 4

    Combining data from multiple MySql tables, in multiple rows, into a single row for a SELECT statement

  5. 5

    Aggregate data from multiple rows into single row

  6. 6

    Select from multiple rows as one row with defaults

  7. 7

    Get row with the lowest id from multiple rows?

  8. 8

    SELECT, format rows with different columns into a single row that share an ID

  9. 9

    How to select multiple rows of mysql table as a single row

  10. 10

    Convert the multiple rows Select statement result into a single row string result

  11. 11

    Select multiple rows into a single row one after the other in Postgres

  12. 12

    SQLite select query to get one row from multiple rows have same id

  13. 13

    combine data of multiple rows from multiple tables and show in single row

  14. 14

    MySQL; Make a single row if multiple rows have the same id

  15. 15

    Select multiple rows from table based on ID

  16. 16

    single ID value from multiple rows with ID's

  17. 17

    How to create multiple rows from single row with time interval in MySQL

  18. 18

    Vectorized implementation to create multiple rows from a single row in pandas dataframe

  19. 19

    SQL Server - Display columns from multiple rows as single row

  20. 20

    How to get columns from multiple rows in a single row in SQL

  21. 21

    How to update a single row from multiple rows with UPDATE JOIN

  22. 22

    SQL Server - Display columns from multiple rows as single row

  23. 23

    How to combine values from multiple rows into a single row in Excel?

  24. 24

    How to combine values from multiple rows into a single row with a comma separator

  25. 25

    How to combine values from multiple rows into a single row using module?

  26. 26

    Attempting to aggregate information from multiple rows into a single row

  27. 27

    Creating multiple rows from single row on specified conditions in hive

  28. 28

    How to get multiple rows from single row of table?

  29. 29

    Postgres Multiple Rows as Single Row

HotTag

Archive