Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Report adds a new year's data to the right of the report - way to automate? Expand / Collapse
Author
Message
Posted Thursday, January 2, 2014 3:18 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, January 30, 2014 2:09 PM
Points: 29, Visits: 135
I have a report that is laid out as such:

Client | 2003 Fees Billed | 2004 Fees Billed | ... | 2013 Fees Billed | 2014 Fees Billed

ABC 500 1000 750 250
XYZ 333 800 243 889


Currently, 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
select distinct client, year, ISNULL(sum(fees_billed),0) as fees_billed
into #a
from client c
join whse w on c.client= w.client
and begdt>=‘1/1/2003’ and enddt<=‘12/31/2013’
group by client, year
order by 1, 3

select distinct client, year, ISNULL(sum(fees_billed),0) as fees_billed
into #b
from client c
join whse w on c.client= w.client
and begdt>=‘1/1/2003’ and enddt<=‘12/31/2003’
group by client, year
order by 1, 3

select distinct client, year, ISNULL(sum(fees_billed),0) as fees_billed
into #c
from client c
join whse w on c.client= w.client
and begdt>=‘1/1/2004’ and enddt<=‘12/31/2004’
group by client, year
order by 1, 3

etc....for the following years

select #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 #a
left 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


If anyone has an idea of how to "elegantly program" this, it would be truly appreciated.

Thanks!
Post #1527307
Posted Thursday, January 2, 2014 3:32 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 6:23 PM
Points: 3,354, Visits: 7,250
Your code seems really unefficient and unconsistent. Here's a better option to do it.
SELECT client, 
SUM( CASE WHEN year = 2003 THEN fees_billed ELSE 0 END) as fees_billed_2003,
SUM( CASE WHEN year = 2004 THEN fees_billed ELSE 0 END) as fees_billed_2004,
SUM( CASE WHEN year = 2005 THEN fees_billed ELSE 0 END) as fees_billed_2005,
--and so on
SUM( CASE WHEN year = 2013 THEN fees_billed ELSE 0 END) as fees_billed_2013
FROM client c
JOIN whse w on c.client= w.client
--and begdt>=‘1/1/2004’ and enddt<=‘12/31/2013’
GROUP BY client
ORDER BY client

It's still static code but it will go through your table just once. From here, you can make the T-SQL dynamic, but you might still need to change the report front-end.
To convert the code into dynamic code, read the following articles:
http://www.sqlservercentral.com/articles/T-SQL/63681/
http://www.sqlservercentral.com/articles/Crosstab/65048/



Luis C.
I am a great believer in luck, and I find the harder I work the more I have of it. Stephen Leacock

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1527308
Posted Sunday, January 5, 2014 7:31 PM
Say Hey Kid

Say Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey Kid

Group: General Forum Members
Last Login: Today @ 1:04 PM
Points: 706, Visits: 4,507
The other way to do it is to use a Matrix in SSRS. You could base your report on a stored procedure that accepts the date range you want and you're off to the races.

Just depends on what you need.
Post #1527956
Posted Thursday, January 16, 2014 9:30 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, January 30, 2014 2:09 PM
Points: 29, Visits: 135
pietlinden,
Thanks for the suggestion on the Matrix report - this was just what I needed.

Post #1531656
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse