Join data from rows to 1 row depending on the number of rows

Grego

I have the following tables:

user
userId   name
1        Sam
2        Harold
3        John


othertable
id      id2      number
1       111      12
1       222      23
1       333      33
2       111      12
2       444      11
3       555      12
3       222      44

The user table's userId matches the othertable's id column. Ideally I'd like to join the content of othertable to user depending on how many rows are present for that id. That's the output I'm aiming for:

e.g.
user
    userId   name    111    222   333   444   555
    1        Sam     12     12    33
    2        Harold  12                 11
    3        John           44                12

Any idea?

Update: The id2's values are limited. Only valid values 111, 222, 333, 444 and 555.

ZHui

you may try out this 1... not sure if it meet you requirement...

CREATE TABLE users 
(
    userId int,
    name varchar(max)
) 

INSERT INTO USERS VALUES
(1, 'Sam'),
(2, 'Harold'),
(3, 'John')

CREATE TABLE othertable 
(
    id int,
    id2 int,
    number int
)

INSERT INTO othertable VALUES
(1, 111, 12),
(1, 222, 23),
(1, 333, 33),
(2, 111, 12),
(2, 444, 11),
(3, 555, 12),
(3, 222, 44)

SELECT
    u.userId,
    u.name,
    SUM(CASE WHEN (id2=111) THEN number ELSE 0 END) AS [111],
    SUM(CASE WHEN (id2=222) THEN number ELSE 0 END) AS [222],
    SUM(CASE WHEN (id2=333) THEN number ELSE 0 END) AS [333],
    SUM(CASE WHEN (id2=444) THEN number ELSE 0 END) AS [444],
    SUM(CASE WHEN (id2=555) THEN number ELSE 0 END) AS [555]
FROM othertable o 
INNER JOIN users u ON o.id = u.userId
GROUP BY u.userId, u.name

Please have a try. :)

UPDATE: Sorry, I'm not really familiar with MySQL. But I did tried my best by changing the query into subquery and hope this can help you. If this doesn't meet you requirement, I hope some other people can help you.

UPDATE 2: Avoid using PIVOT

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Remove ith row of a matrix. i varies from 1 to number of rows in data matrix

From Dev

Is there a simple way join rows by their row number in mysql?

From Dev

Is there a simple way join rows by their row number in mysql?

From Dev

Validate number of rows depending on master row field value

From Dev

Join 1 Row to Multiple Rows in PDO

From Dev

Join 1 Row to Multiple Rows in PDO

From Dev

MySQL Update 1 row from multiple rows of data with same key

From Dev

MySQL Update 1 row from multiple rows of data with same key

From Dev

Inflate views of rows depending on the type of data in each row of RecyclerView

From Dev

Inflate views of rows depending on the type of data in each row of RecyclerView

From Dev

Erase rows from a cell array depending on empty cells in each row

From Dev

mySQL - Combining result from rows depending on count in first row?

From Dev

Copy specific cells from sertain rows from Sheet 1 depending on non-zero value in column to the last empty row of Sheet 2

From Dev

Get 3 rows data in 1 row

From Dev

Extracting rows from a data frame depending on the combination of values in two colums

From Dev

Aggregate data from multiple rows into single row

From Dev

Getting data from particular number of rows

From Dev

In temporary tables/variables: join multiple rows to table with only 1 row

From Dev

MySQL JOIN returning only 1 row out of several rows

From Dev

Pandas: join multiple columns of one row to many rows (1:n)

From Dev

SQL Query to get 2 rows data into single row with join

From Dev

Filter out rows not columns depending on values in the row

From Dev

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

From Dev

Join three rows with one row from another table

From Dev

Fetch single row from two rows of a table with join in SQL Server

From Dev

Join a dynamic number of rows in postgres

From Dev

select data.table R rows based on row number and condition

From Dev

Move data from row to another row within a group of specified rows

From Dev

Can I create a tibble with 1 row and 11 columns in R from a data frame with 0 rows and 0 columns?

Related Related

  1. 1

    Remove ith row of a matrix. i varies from 1 to number of rows in data matrix

  2. 2

    Is there a simple way join rows by their row number in mysql?

  3. 3

    Is there a simple way join rows by their row number in mysql?

  4. 4

    Validate number of rows depending on master row field value

  5. 5

    Join 1 Row to Multiple Rows in PDO

  6. 6

    Join 1 Row to Multiple Rows in PDO

  7. 7

    MySQL Update 1 row from multiple rows of data with same key

  8. 8

    MySQL Update 1 row from multiple rows of data with same key

  9. 9

    Inflate views of rows depending on the type of data in each row of RecyclerView

  10. 10

    Inflate views of rows depending on the type of data in each row of RecyclerView

  11. 11

    Erase rows from a cell array depending on empty cells in each row

  12. 12

    mySQL - Combining result from rows depending on count in first row?

  13. 13

    Copy specific cells from sertain rows from Sheet 1 depending on non-zero value in column to the last empty row of Sheet 2

  14. 14

    Get 3 rows data in 1 row

  15. 15

    Extracting rows from a data frame depending on the combination of values in two colums

  16. 16

    Aggregate data from multiple rows into single row

  17. 17

    Getting data from particular number of rows

  18. 18

    In temporary tables/variables: join multiple rows to table with only 1 row

  19. 19

    MySQL JOIN returning only 1 row out of several rows

  20. 20

    Pandas: join multiple columns of one row to many rows (1:n)

  21. 21

    SQL Query to get 2 rows data into single row with join

  22. 22

    Filter out rows not columns depending on values in the row

  23. 23

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

  24. 24

    Join three rows with one row from another table

  25. 25

    Fetch single row from two rows of a table with join in SQL Server

  26. 26

    Join a dynamic number of rows in postgres

  27. 27

    select data.table R rows based on row number and condition

  28. 28

    Move data from row to another row within a group of specified rows

  29. 29

    Can I create a tibble with 1 row and 11 columns in R from a data frame with 0 rows and 0 columns?

HotTag

Archive