Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
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.
Hi All,I am using sql server 2005.I have one fuction that calculate the totalcall made by employee during specified period. But this function taking much time for longer durationIs There Any way to remove function and Get the desired result in query itselfI have created index on HDr id MY tables are Header tableHDRID reportingdate empid 1 01-12-2010 EMP1 2 02-12-2010 EMP2Details TableHDRID CustomerID Product1 Cust1 Prod11 Cust1 Prod21 Cust2 Prod1 1 Cust3 Prod11 Cust4 Prod11 Cust4 Prod2Now When i calculate Total call done by EMP1 it should take customerid for specified period and count and result should come as four.function is ALTER FUNCTION [dbo].[Gettotalcall](@from_dt datetime,@to_dt datetime , @empcode varchar(50) )RETURNS INTAS BEGIN DECLARE@totalcall intselect @totalcall = count(*) from(select empid , customeridFROM Header A INNER JOIN Details B ON A.Hdrid = B.HdridWHERE empid =@empcode AND (reportingdate BETWEEN @stdate AND @enddate) group by reportingdate , CustomerID )xreturn @totalcall end and i fetch it as select empid , dbo.[[Gettotalcall]]( @stdate ,@enddate , @empod )FROM Header A INNER JOIN Details B ON A.Hdrid = B.HdridWHERE empid IN (SELECT Emp FROM @tbl) AND (reportingdate BETWEEN @stdate AND @enddate) Thank you
Sachin.Nand
2937 Posts
Posted - 2010-12-28 : 04:26:16
Won't just this suffice your requierment?
select empid , dbo.[[Gettotalcall]]( @stdate ,@enddate , @empod ),COUNT(distinct CustomerID)FROM Header A INNER JOIN Details B ON A.Hdrid = B.HdridWHERE empid IN (SELECT Emp FROM @tbl) AND (reportingdate BETWEEN @stdate AND @enddate) group by empid
PBUH
Sachin.Nand
2937 Posts
Posted - 2010-12-28 : 04:29:15
As far as indexes are concered I suggest you to create a composite non clustered index on empid,reportingdate covering CustomerIDPBUH