Question

Came to know its called pivots, but couldn't get examples for sqlite without aggregation.

Schema:

Readonly Table foos(_id, foo) (10 max records)
Readonly Table bars(_id, bar) (300 max records)
Readonly Table items(_id, foo_id, bar_id, item1, item2) (3000 max records)

Output Required:

Select
 foos._id, 
 foos.foo, 
 bars.bar, 
 items.item1(bar_id=1), 
 items.item1(bar_id=2), 
 items.item2(bar_id=1) 

i always need two columns.

Am i missing something very obvious. do i get a performance hit. is my table design wrong, correct me!

UPDATE:

SELECT
    foo_id,
    GROUP_CONCAT(CASE bar_id WHEN 1 THEN title ELSE NULL END) AS 'Item1',
    GROUP_CONCAT(CASE bar_id WHEN 2 THEN title ELSE NULL END) AS 'Item2'
FROM 
    items
WHERE 
    foo_id=2
GROUP BY
    foo_id

this is what is ended up with.

Was it helpful?

Solution

You should check out the Common MySql Queries, there are a lot of samples, including the pivots, which may help you.

You can find what you are looking for in this section.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top