jeudi 1 octobre 2015

Multiple row data transformed into single

I'm using SQLite and I"m really green. Essentially I have a table (example below)

ID  Date    QuestNum    Value
1   1/1/1990    1   0
1   1/1/1990    2   3
1   1/1/1990    3   2
1   1/2/1990    1   5
1   1/2/1990    2   2
1   1/2/1990    3   6
2   1/1/1990    1   6
2   1/1/1990    2   3
2   1/1/1990    3   6
2   1/2/1990    1   2
2   1/2/1990    2   8
2   1/2/1990    3   2

And I would like to query another table that looks like this

ID  Date    Quest1  Quest2  Quest3
1   1/1/1990    0   3   2
1   1/2/1990    5   2   6
2   1/1/1990    6   3   6
2   1/2/1990    2   8   2

The closest I got was

SELECT ID,Date,
MAX(CASE WHEN QuestNum = "1" THEN Value END) AS Q1,
MAX(CASE WHEN QuestNum = "2" THEN Value END) AS Q2,
MAX(CASE WHEN QuestNum = "3" THEN Value END) AS Q3,
FROM table
GROUP BY subjID, Date

Although this does provide the table structure I'm looking for it applies the MAX aggregate function and I'm worried it altered the data.

I'm not sure if it is important to note but the QuestNum can skip at times, so that not everyone receives all questions.

Thanks, qwerty

Aucun commentaire:

Enregistrer un commentaire