SQL combine multiple rows into 1 row

Samatag

I am trying to take rows with the same ID and return them on the same row. My data looks like the follow:

ID  Fruit
1   Banana
1   Apple
1   Grapefruit
2   Cherry
2   Blueberry
3   Lime
3   Pear

And I would like it to look like this:

ID  Fruit   Fruit1  Fruit2
1   Banana  Apple   Grapefruit
2   Cherry  Blueberry   NULL

I have tried this as a query, but I don't seem to be having much luck:

SELECT a.[ID],a.[Fruit],b.[Fruit]
FROM [test].[dbo].[Fruit] a
JOIN [test].[dbo].[Fruit] b
ON a.ID = b.ID
WHERE a.FRUIT <> b.FRUIT

Can anybody help with this?

Thanks!

ollie

You can use combination of a windowing function like row_number and then some conditional aggregation using a CASE expression with MAX() to get the result that you want:

select 
  Id,
  Fruit = max(case when rn = 1 then Fruit end),
  Fruit1 = max(case when rn = 2 then Fruit end),
  Fruit2 = max(case when rn = 3 then Fruit end)
from
(
  select 
    Id, 
    Fruit,
    rn = row_number() over(partition by Id order by Id)
  from [test].[dbo].[Fruit]
) d
group by Id;

See a Demo. The row_number() function creates a unique number for each id, then using this number along with CASE and MAX you will convert your rows of data into columns.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Multiple rows into a single row and combine column SQL

From Dev

SQL Server Combine multiple rows to one row with multiple columns

From Dev

T-SQL How to combine multiple rows into a single row

From Dev

Combine Multiple Rows into One Row (One Column) when Using SQL

From Dev

T-SQL How to combine multiple rows into a single row

From Dev

Combine multiple rows to one row

From Dev

Combine SQL rows into one row

From Dev

SQL Combine multiple row fields into 1 record based on different field

From Dev

SQL Combine multiple rows into one

From Dev

SQL Combine multiple rows from one table into one row with multiple columns

From Dev

How to combine multiple rows into one rows in SQL

From Dev

MySQL combine rows from multiple tables into on row

From Dev

Combine multiple rows into one row MySQL

From Dev

How to combine multiple rows into a single row with pandas

From Dev

Combine multiple MYSQL rows into one row

From Dev

how combine multiple rows into a single row in Oracle?

From Dev

SQL Combine Multiple rows into Multiple Columns

From Dev

How to combine rows into one row in SQL?

From Dev

SQL - combine rows conditionally on the value of previous row

From Dev

T-SQL: Combine rows to one row

From Dev

Using SQL SMS, how do I combine multiple rows into one row, but maintain all the column data?

From Dev

How to combine multiple rows from 4 tables into one single row in a new table in SQL?

From Dev

Combine multiple result rows into one in SQL

From Dev

SQL - combine multiple rows with similar username

From Dev

Combine multiple result rows into one in SQL

From Dev

How to combine multiple rows in T-SQL

From Dev

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

From Dev

How combine multiple cells into a single row in SQL?

From Dev

SQL Combine 2 Different Rows into 1

Related Related

  1. 1

    Multiple rows into a single row and combine column SQL

  2. 2

    SQL Server Combine multiple rows to one row with multiple columns

  3. 3

    T-SQL How to combine multiple rows into a single row

  4. 4

    Combine Multiple Rows into One Row (One Column) when Using SQL

  5. 5

    T-SQL How to combine multiple rows into a single row

  6. 6

    Combine multiple rows to one row

  7. 7

    Combine SQL rows into one row

  8. 8

    SQL Combine multiple row fields into 1 record based on different field

  9. 9

    SQL Combine multiple rows into one

  10. 10

    SQL Combine multiple rows from one table into one row with multiple columns

  11. 11

    How to combine multiple rows into one rows in SQL

  12. 12

    MySQL combine rows from multiple tables into on row

  13. 13

    Combine multiple rows into one row MySQL

  14. 14

    How to combine multiple rows into a single row with pandas

  15. 15

    Combine multiple MYSQL rows into one row

  16. 16

    how combine multiple rows into a single row in Oracle?

  17. 17

    SQL Combine Multiple rows into Multiple Columns

  18. 18

    How to combine rows into one row in SQL?

  19. 19

    SQL - combine rows conditionally on the value of previous row

  20. 20

    T-SQL: Combine rows to one row

  21. 21

    Using SQL SMS, how do I combine multiple rows into one row, but maintain all the column data?

  22. 22

    How to combine multiple rows from 4 tables into one single row in a new table in SQL?

  23. 23

    Combine multiple result rows into one in SQL

  24. 24

    SQL - combine multiple rows with similar username

  25. 25

    Combine multiple result rows into one in SQL

  26. 26

    How to combine multiple rows in T-SQL

  27. 27

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

  28. 28

    How combine multiple cells into a single row in SQL?

  29. 29

    SQL Combine 2 Different Rows into 1

HotTag

Archive