SQLTeam.com | Weblogs | Forums

Add missing quarter details in rows

tsql
sql2008

#1

Hi,

I am stuck here trying to add missing quater details per customer record - I have tried to highlight data below with expected output .Is there a way this can be efficiently implemented using SQL atleast till part 1 below?

trying to figure out how to add missing quarters (example can be 2012 3rd qtr to 2015 first qtr)

Data :

Customer Quarter Year Amount
120 2 2015 1000
120 3 2015 1000
121 1 2015 1000
121 3 2016 1000
121 4 2016 1000
122 1 2015 1000
122 2 2015 1000

Expected --Part 1

Customer Quarter Year Amount
120 1 2015 0
120 2 2015 1000
120 3 2015 1000
120 4 2015 0
120 1 2016 0
120 2 2016 0
120 3 2016 0
120 4 2016 0

121 1 2015 1000
121 2 2015 0
121 3 2015 0
121 4 2015 0
121 1 2016 0
121 2 2016 0
121 3 2016 1000
121 4 2016 1000

122 1 2015 1000
122 2 2015 1000
122 3 2015 0
122 4 2015 0
122 1 2016 0
122 2 2016 0
122 3 2016 1000
122 4 2016 1000

Expected -- Part 2

120 Q12015 - q2 2015 ......Q42016
121
122