mercredi 26 août 2015

sql If statement or case

I think I may be missing the obvious here but im trying to create a sql query to pull data out in a particular way but cant work it out.

I have a table which is made up with the following columns:

Name, StageDate, Stage

I want to create in sql an output something like this:

Name, Stage, Stage1Date, Stage2Date, Stage3Date

Person1, 1, 01/01/2015, NULL, NULL

Person1, 2, 01/01/2015, 02/01/2015, NULL

Person1, 3, 01/01/2015, 02/01/2015, 03/01/2015

My query is currently as follows:

select Name
        , Stage
        , Case when Stage = 1 then StageDate end as Stage1Date
        , Case when Stage = 2 then StageDate end as Stage2Date
        , Case when Stage = 3 then StageDate end as Stage3Date
From Details

Data export for the above query currently looks like this:

Name, Stage, Stage1Date, Stage2Date, Stage3Date

Person1, 1, 01/01/2015, NULL, NULL

Person1, 2, NULL, 02/01/2015, NULL

Person1, 3, NULL, NULL, 03/01/2015

Aucun commentaire:

Enregistrer un commentaire