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

Mally

If I have data where the date and time are the same or different as below:

ID    Date               LOC
1     2015-12-02 10:05   A
1     2015-12-02 10:05   B2
2     2015-12-02 10:05   D
2     2015-12-02 10:05   A7P 
2     2015-12-02 10:06   AD

Is there any way of showing the following:

ID   DATE1             LOC1  DATE 2            LOC2  DATE 3            LOC3
1    2015-12-02 10:05  A     2015-12-02 10:05  B2
2    2015-12-02 10:05  D     2015-12-02 10:05  A7P   
2    2015-12-02 10:06  AD

So there will be multiple rows where the ID is the same but where the Date and time are different?

I have used the partition example below and this works perfectly for getting data into one row as I originally asked. But is it possible to show multiple rows when the ID is the same but where the date and time are different.

BELOW IS THE ORIGINAL QUESTION THAT HAS BEEN ANSWERED..

I've looked thrrough all of the answers relating to this question but cant find any code that works and a lot of the questions relate to two tables.

I have one table with multiple rows of data and multiple columns containing different data types e.g.

ID    Date               LOC
1     2015-11-05 10:05   A
1     2015-12-02 10:06   B2
2     2015-12-02 10:05   D
2     2015-12-02 10:05   A7P 
2     2015-12-02 10:06   AD

I simply require one row for each ID with all of the data in multiple columns e.g.

ID   DATE1             LOC1  DATE 2            LOC2  DATE 3            LOC3
1    2015-11-05 10:05  A     2015-12-02 10:06  B2
2    2015-12-02 10:05  D     2015-12-02 10:05  A7P   2015-12-02 10:06  AD

These can have duplicated data and one or more rows for the same ID.

I've tried a few pivot/unpivot sql codes but i get an error about different types in the unpivot.

Any help would be greatly appreciated.

Hogan

This is the basic way to this, first make row_number and then do a join

WITH TAB_RN AS
(
  SELECT ID, Date, LOC, ROW_NUMBER() OVER (PARTITION BY ID, Date ORDER BY LOC) AS RN
  FROM YOUR_TABLE
)
SELECT T1.ID, 
       T1.Date AS DATE1, T1.LOC AS LOC1,
       T2.Date AS DATE2, T2.LOC AS LOC2,
       T3.Date AS DATE3, T3.LOC AS LOC3
FROM TAB_RN T1
LEFT JOIN TAB_RN T2 ON T1.ID = T2.ID AND T1.Date = T2.Date AND T2.RN = 2
LEFT JOIN TAB_RN T3 ON T1.ID = T3.ID AND T1.Date = T2.Date AND T3.RN = 3
WHERE T1.RN = 1

If you don't know "how many" there will be then you have to use this as a template to make dynamic sql.

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 Server Combine multiple rows to one row with multiple columns

From Dev

Trying to combine multiple rows from a table into one row

From Dev

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

From Dev

Combine multiple rows in one from same table

From Dev

Combine multiple rows in one from same table

From Dev

Combine multiple rows to one row

From Dev

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

From Dev

Combine multiple rows into one row to replace null values in columns

From Dev

Combine multiple rows into one row with same number of columns

From Dev

SQL Combine multiple rows into one

From Dev

How to combine multiple rows into one rows in SQL

From Dev

Combine multiple rows into one row MySQL

From Dev

Combine multiple MYSQL rows into one row

From Dev

Combine multiple rows into one

From Dev

SQL : How to copy one row in a table in new table with multiple columns

From Dev

SQL Multiple rows/records into one row with 3 columns

From Dev

sql split one row to multiple rows based on number of columns available

From Dev

SQL select one row from table 1 joining on multiple rows in table 2

From Dev

Combine multiple result rows into one in SQL

From Dev

Combine multiple result rows into one in SQL

From Dev

How to combine multiple columns into one column in SQL?

From Dev

move multiple columns to rows and one row as column

From Dev

Create view from multiple tables, combine values from multiple rows into one row

From Dev

Combine multiple rows into one row MySQL and split value from one field to two in same Query

From Dev

sql query select only one row from multiple rows

From Dev

how to select one row from one table and multiple rows from other table using joins in mysql,

From Dev

Combine SQL rows into one row

From Dev

Multiple rows to one row Oracle SQL

From Dev

combining multiple rows into one row in sql server

Related Related

  1. 1

    SQL Server Combine multiple rows to one row with multiple columns

  2. 2

    Trying to combine multiple rows from a table into one row

  3. 3

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

  4. 4

    Combine multiple rows in one from same table

  5. 5

    Combine multiple rows in one from same table

  6. 6

    Combine multiple rows to one row

  7. 7

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

  8. 8

    Combine multiple rows into one row to replace null values in columns

  9. 9

    Combine multiple rows into one row with same number of columns

  10. 10

    SQL Combine multiple rows into one

  11. 11

    How to combine multiple rows into one rows in SQL

  12. 12

    Combine multiple rows into one row MySQL

  13. 13

    Combine multiple MYSQL rows into one row

  14. 14

    Combine multiple rows into one

  15. 15

    SQL : How to copy one row in a table in new table with multiple columns

  16. 16

    SQL Multiple rows/records into one row with 3 columns

  17. 17

    sql split one row to multiple rows based on number of columns available

  18. 18

    SQL select one row from table 1 joining on multiple rows in table 2

  19. 19

    Combine multiple result rows into one in SQL

  20. 20

    Combine multiple result rows into one in SQL

  21. 21

    How to combine multiple columns into one column in SQL?

  22. 22

    move multiple columns to rows and one row as column

  23. 23

    Create view from multiple tables, combine values from multiple rows into one row

  24. 24

    Combine multiple rows into one row MySQL and split value from one field to two in same Query

  25. 25

    sql query select only one row from multiple rows

  26. 26

    how to select one row from one table and multiple rows from other table using joins in mysql,

  27. 27

    Combine SQL rows into one row

  28. 28

    Multiple rows to one row Oracle SQL

  29. 29

    combining multiple rows into one row in sql server

HotTag

Archive