lundi 10 juillet 2017

Checking columns with case statement

I have a Table with the following column, in SQL Server 2012

ID YEARNBR
1   2016
2   2015
3   2014
4   2013
1   2015
1   2014
2   2016

I wanted to create a case statement that would check this column YEARNBR.

(case when yearNBR =2017 and YearNBR = 2016 and YearNBR = 2015 and yearNBR = 2014 then 1 else 0 end)

if my column YEARNBR would contain the year 2016,2015, and 2014 and I would be grouping by ID, then I would want another column that would be called: 2014Through2016 that would contain the value 1.

ID  2014Through2016    2015Through2016  2016
1    1                      1             1
2    0                      1             1
3    0                      0             0
4    0                      0             0

Any help would be appreciated !!

Aucun commentaire:

Enregistrer un commentaire