mardi 10 décembre 2019

SQL how to select multiple columns based on "earlier date" condition

I am trying to convert the table below using SQL:

person_id event1 date1        event2   date2
1         yes    2019-01-01   NULL     NULL
2         NULL   NULL         yes      2019-02-02
3         NULL   NULL         NULL     NULL
4         yes    2019-03-01   yes      2019-03-04

Into something like this:

person_id  event_overall  date_overall
1          event1         2019-01-01
2          event2         2019-02-02
3          no_event       NULL
4          event1         2019-03-01

Basically, if event1 is "yes" and event2 is "NULL, then event1 is shown, and vice versa. If both are "NULL", then "no_event" is shown. If both are "yes", then the earlier date (in the example 2019-03-01 comes before 2019-03-04, so event1 is selected) is shown.

Kindly help please. Thanks!

Aucun commentaire:

Enregistrer un commentaire