I have a report that is laid out as such:Client | 2003 Fees Billed | 2004 Fees Billed | ... | 2013 Fees Billed | 2014 Fees BilledABC 500 1000 750 250XYZ 333 800 243 889Currently, I'm running this report manually each month, and capturing any new fees billed to the specific clients. I'd like to automate this report and get me out of doing it each month. I capture the amount billed for the total time period (2003 - 2014) and then break out each year by itself. As we are rolling over to 2014, a new column of data will need to be added. And going forward as 2015 comes around, etc...these will also be new columns added to the right of 2014. I'd like to automate this if possible, but I'm not quite sure how to accomplish this. I know I could add the future years to the report now, add code for the years for when data does exist, and only show those future years if data is there. However, that's probably not the most efficient or clean.Current SQL Code for the report[code="sql"]select distinct client, year, ISNULL(sum(fees_billed),0) as fees_billedinto #afrom client cjoin whse w on c.client= w.clientand begdt>=‘1/1/2003’ and enddt<=‘12/31/2013’group by client, yearorder by 1, 3select distinct client, year, ISNULL(sum(fees_billed),0) as fees_billedinto #bfrom client cjoin whse w on c.client= w.clientand begdt>=‘1/1/2003’ and enddt<=‘12/31/2003’group by client, yearorder by 1, 3select distinct client, year, ISNULL(sum(fees_billed),0) as fees_billedinto #cfrom client cjoin whse w on c.client= w.clientand begdt>=‘1/1/2004’ and enddt<=‘12/31/2004’group by client, yearorder by 1, 3etc....for the following yearsselect #a.*, #b.begdt+’03’, ISNULL(#b.fees_billed,0), #c.begdt+’04’, ISNULL(#c.fees_billed,0),#d.begdt+’05’, ISNULL(#d.fees_billed,0), #e.begdt+’06’, ISNULL(#e.fees_billed,0), #f.begdt+’07’, ISNULL(#f.fees_billed,0), #g.begdt+’08’, ISNULL(#g.fees_billed,0),#h.begdt+’09’, ISNULL(#h.fees_billed,0), #i.begdt+’10’, ISNULL(#i.fees_billed,0), #j.begdt+’11’, ISNULL(#j.fees_billed,0), #k.begdt+’12’, ISNULL(#k.fees_billed,0), #l.begdt+’13’, ISNULL(#l.fees_billed,0)from #aleft outer join #b on #a.client = #b.client and left(#a.begdt,6) = left(#b.begdt,6)left outer join #c on #a.client = #c.client and left(#a.begdt,6) = left(#c.begdt,6)left outer join #d on #a.client = #d.client and left(#a.begdt,6) = left(#d.begdt,6)left outer join #e on #a.client = #e.client and left(#a.begdt,6) = left(#e.begdt,6)left outer join #f on #a.client = #f.client and left(#a.begdt,6) = left(#f.begdt,6)left outer join #g on #a.client = #g.client and left(#a.begdt,6) = left(#g.begdt,6)left outer join #h on #a.client = #h.client and left(#a.begdt,6) = left(#h.begdt,6)left outer join #i on #a.client = #i.client and left(#a.begdt,6) = left(#i.begdt,6)left outer join #j on #a.client = #j.client and left(#a.begdt,6) = left(#j.begdt,6)left outer join #k on #a.client = #k.client and left(#a.begdt,6) = left(#k.begdt,6)left outer join #l on #a.client = #l.client and left(#a.begdt,6) = left(#l.begdt,6)order by 1[/code]If anyone has an idea of how to "elegantly program" this, it would be truly appreciated.Thanks!
↧