I am pulling customer numbers and renewal dates (policy term start dates) from a table in SQL and trying to generate a new table with a single row for each customer.
if i just pull customer numbers and the term effective dates, i get multiple rows for each customer for every unique term date: e.g.
-customer 1 5/12/2016
-customer 1 5/12/2017
-customer 2 8/27/2012
-customer 2 8/27/2013
-customer 2 8/27/2014
-customer 2 8/27/2015
-customer 2 8/27/2016
-customer 2 8/27/2017
-customer 3 3/25/2012
-customer 3 3/25/2013
-customer 3 3/25/2014
-customer 3 3/25/2015
I would like to automatically generate the data where each customer is on a single row with multiple columns representing the year of their respective terms. e.g.:
-customer 1 5/12/2016
-customer 2 8/27/2012 8/27/2013 8/27/2014 8/27/2015 8/27/2016
-customer 3 3/25/2012 3/25/2013 3/25/2014 3/25/2015
My code in SQL looks like this:
SELECT
CUSTOMER,
case when Year(TERM_EFFECTIVE_DATE) = 2012 then TERM_EFFECTIVE_DATE end as "2012",
case when Year(TERM_EFFECTIVE_DATE) = 2013 then TERM_EFFECTIVE_DATE end as "2013",
etc
But my output is giving me multiple rows for each customer and null values in the columns where the years did not match. e.g.:
-customer 1 N/A N/A N/A N/A 5/12/2016
-customer 1 N/A N/A N/A N/A N/A
-customer 2 8/27/2012 N/A N/A N/A N/A
-customer 2 N/A 8/27/2013 N/A N/A N/A
-customer 2 N/A N/A 8/27/2014 N/A N/A
-customer 2 N/A N/A N/A 8/27/2015 N/A
-customer 2 N/A N/A N/A N/A 8/27/2016
-customer 2 N/A N/A N/A N/A N/A
-customer 3 3/25/2012 N/A N/A N/A N/A
-customer 3 N/A 3/25/2013 N/A N/A N/A
-customer 3 N/A N/A 3/25/2014 N/A N/A
-customer 3 N/A N/A N/A 3/25/2015 N/A
-customer 3 N/A N/A N/A N/A 3/25/2016
Is there a way i can fix this and reduce the rows to one per customer?