SQLite : How to combine subquery results

I have a table like this

    id  |   inventory_id    |   serial_type |   serial
------------------------------------------------------
    38  |       5892        |       1       |   9502
    39  |       5895        |       1       |   i95054-1
    40  |       5895        |       2       |   m95054-1
    41  |       5895        |       1       |   i95054-2
    42  |       5895        |       2       |   m95054-2
    43  |       5895        |       1       |   i95054-3
    44  |       5895        |       2       |   m95054-3
    45  |       5895        |       1       |   i95054-4
    46  |       5895        |       2       |   m95054-4

I have 2 subqueries from same table like

SELECT inventory_id, serial as type_one_serial
FROM serials
WHERE serial_type = 1
AND inventory_id = 5895

and another

SELECT inventory_id, serial as type_two_serial
FROM serials
WHERE serial_type = 2
AND inventory_id = 5895

How I can combine these to get a result like

inventory_id | type_one_serial | type_two_serial
------------------------------------------------
5895         |  i95054-1       |    m95054-1
5895         |  i95054-2       |    m95054-2
5895         |  i95054-3       |    m95054-3
5895         |  i95054-4       |    m95054-4

EDIT : I have modified the source table to iclude another column sl

    sl  |   id  |   inventory_id    |   serial_type |   serial
 ----------------------------------------------------------------
    0   |   38  |       5892        |       1       |   9502
    0   |   39  |       5895        |       1       |   i95054-1
    0   |   40  |       5895        |       2       |   m95054-1
    1   |   41  |       5895        |       1       |   i95054-2
    1   |   42  |       5895        |       2       |   m95054-2
    2   |   43  |       5895        |       1       |   i95054-3
    2   |   44  |       5895        |       2       |   m95054-3
    3   |   45  |       5895        |       1       |   i95054-4
    3   |   46  |       5895        |       2       |   m95054-4

Upvotes: 3

Views: 219

Answers (2)

@Tim Biegeleisen 's edited answer worked perfectly for me. I have done few modifications based on that. Not sure it will make any improvement, just posting here for reference :

SELECT inventory_id, MAX(CASE WHEN serial_type = 1 THEN serial END) AS type_one_serial, MAX(CASE WHEN serial_type = 2 THEN serial END) AS type_two_serial FROM serials WHERE inventory_id = 5895 GROUP BY sl

Upvotes: 0

Tim Biegeleisen
Tim Biegeleisen

Reputation: 520968

You are looking for a pivot query:

SELECT inventory_id,
       MAX(CASE WHEN serial_type = 1 THEN 'i' || SUBSTR(serial, 2) END) AS type_one_serial,
       MAX(CASE WHEN serial_type = 2 THEN 'm' || SUBSTR(serial, 2) END) AS type_two_serial
FROM serials
WHERE serial_type IN (1, 2) AND      -- the WHERE clause may be optional
      inventory_id = 5895            -- depending on what you want
GROUP BY inventory_id,
         SUBSTR(serial, 2)

Update:

Taking the new sl column into account, we can use this to discriminate between the two types of serials for a given inventory_id. In this case, we could write the following query:

SELECT inventory_id,
       MAX(CASE WHEN serial_type = 1 THEN serial END) AS type_one_serial,
       MAX(CASE WHEN serial_type = 2 THEN serial END) AS type_two_serial
FROM serials
GROUP BY inventory_id,
         sl

Upvotes: 4

Related Questions