Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
HelalM
Starting Member
19 Posts |
Posted - 2010-10-06 : 15:44:50
|
| I inherited these codes and optimized it to the extent of my SQL knowledge which doesn't go very far! I reduced the temp tables from 9 to 6, but I need further optimization (efficiency/performance) for SSRS report development. Here is the code: Set NOCOUNT ONdeclare @StartDate char(10)declare @EndDate char(10)set @StartDate = '09-01-2010'set @EndDate = '09-30-2010'-----------------if object_id('tempdb..#calltrack') is not null begin drop table #calltrack endSELECT DISTINCT calltr.calldate ,callr.status ,callr.resoldate ,datename(dw, calltr.calldate) as calldate_day ,callt.category ,callt.description as calltypedesc ,calls.description as callsource ,calltr.createid ,callr.createdate ,calltr.memid ,mem.fullname as memname ,CAST(calln.callnote as VARCHAR(500)) as callnoteINTO #calltrackFROM callreason callr with (nolock) inner join calltype callt with (nolock) on callr.callcode = callt.callcode inner join calltrack calltr with (nolock) on calltr.callerid = callr.callerid inner join callsource calls with (nolock) on calls.callsourceid = calltr.callsourceid inner join member mem with (nolock) on mem.memid = calltr.memid inner join callnote calln with (nolock) on calln.callerid = calltr.callerid Where callr.status in ('X', 'Y') and calltr.calldate between @StartDate and @EndDate and calltr.createid in ('X', 'Y' , 'Z')order by mem.fullname-----------------------------------------------------------------------------------------------BROUGHT BACK MAX TERM AND EFFECTIVE DATE FOR MEMBERS IN APPOPRIATE PROGRAMif object_id('tempdb..#maxterm') is not null begin drop table #maxterm endselect distinct memid, max(termdate) as termdateinto #maxterm from enrollkeys where segtype='INT'group by memidif object_id('tempdb..#maxterm2') is not null begin drop table #maxterm2 endselect distinct m.memid, m.termdate, max(effdate) as effdateINTO #maxterm2 from #maxterm m inner join enrollkeys e with (nolock) ON m.memid=e.memid AND m.termdate=e.termdate AND segtype='INT' group by m.memid, m.termdate--THERE ARE FEW MEMBERS WHO ARE IN TWO DIFFERENT PROGRAMSif object_id('tempdb..#maxenrollid') is not null begin drop table #maxenrollid endselect distinct m.memid, m.termdate, bp.programid, p.description as programinto #maxenrollidfrom #maxterm2 minner join enrollkeys e with (nolock) ON m.memid=e.memid and m.termdate=e.termdate AND m.effdate=e.effdate AND segtype='INT' inner join benefitplan bp with (nolock) ON e.planid=bp.planid AND bp.programid IN ('X' , 'Y', 'Z') inner join program p with (nolock) ON bp.programid=p.programid--------------------------------------------------------------------------------------------if object_id('tempdb..#calltrack2') is not null begin drop table #calltrack2 endSELECT DISTINCT calldate, status, resoldate, calldate_day, category, calltypedesc, callsource, createid, createdate, c.memid, memname, program, callnoteINTO #calltrack2 FROM #calltrack c INNER JOIN #maxenrollid m with (nolock) ON c.memid=m.memid if object_id('tempdb..#calltrack3') is not null begin drop table #calltrack3 endSELECT DISTINCT calldate, memid, callnote, max(createdate) as createdateINTO #calltrack3FROM #calltrack2GROUP BY calldate, memid, callnoteSELECT DISTINCT c2.calldate, c2.status, c2.resoldate, c2.calldate_day, c2.category, c2.calltypedesc, c2.callsource, c2.createid, c3.createdate, c2.memid, c2.memname, c2.program, c2.callnote FROM #calltrack2 c2 INNER JOIN #calltrack3 c3 with (nolock) ON c2.calldate=c3.calldate AND c2.memid=c3.memid AND c2.callnote=c3.callnote AND c2.createdate=c3.createdateSince I am here (newbie) please be more specific in your suggestions. Give me examples if possible.Thanks,HelalHM |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-10-17 : 04:47:32
|
| without understanding what exactly trying to do in report its hard to suggest. I dont think anybody will take pain of going through whole code and then understand what you want. So please post some sample data from tables and then explain what you're trying achieve from them in report?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
HelalM
Starting Member
19 Posts |
Posted - 2010-10-20 : 10:23:09
|
| I total y undrestand and would have if I had errors with the script. My question is rather general. The script runs ok and I get my results ok as well. However, when developing this report for SSRS, then there is a rendering issue as I was told by IT department. Thus, the query needs to be optimized and temp tables be removed as much as possible by using drive queries, etc....as I indicated, I have reduced using temp tables ffrom 9 to 6 but need help to use other methods and perhaps completely remove all temp tables. As I was reviewing these codes, there seem to be some redundancies and those could be optimized. In sum, what I need is to optimize this script. The result should look like:Columns:calldate, status, resoldate, calldate_day, category, calltypedesc, callsource,creatid,createdate,memid,memname,program,callnoteHM |
 |
|
|
|
|
|
|
|