Hi, I was wondering if it was possible to determine from which column a coalesce value is drawn from?
I have the following example data (actual years range from 1989 - 2010 in data, not shown for brevity)
ID | 2000 | 2000 value | 2001 |2001 value | 2002 |2002 value | 2003 |2003 value | 2004 | 2004 value | 2005 | 2005 value
id001 | single | 15.46 |regular|50 | NULL | 0 |single | 152 | regular|15.20 |single| 15.99
id002 | regular | 20.46 |regular|17.99 |single | 150.23 |both | 256.3 | NULL | 0 | NULL | 0
Where single / regular / both reflect how that ID paid for something in that year (and NULL represents no purchases).
What I would ideally like to have is a three columns per year for the years 2005-2010 tells you the most recent single payment type before that year (and in which year it falls), as well as a column for regular and both payment types
So for the example above the results would look like:
ID | 2005 prior single year | 2005 prior regular year | 2005 prior both year
id001 | 2003 | 2004 | NULL
id002 | 2002 | 2001 | 2003
I would also like to be able to pull out the respective values as well (for all years 2005-2010).
Fundamentally it's just a case of looking across columns to find the first instance, but beyond some kind of coalesce I'm not sure how best to approach this!
Thanks! :)