Rashednadeem
Starting Member
5 Posts |
Posted - 2014-07-14 : 14:09:19
|
Please Help to optimize this SPALTER PROCEDURE [dbo].[Att_AttendanceReport] -- Variables@EMPLOYEEID INT =NULL,@DEPARTMENTID INT =NULL,@SECTIONID INT =NULL,@FROMDATE datetime =NULL,@TODATE datetime=NULL,@Status varchar(30)=NullASdeclare @holiday varchar(30)=NULL -- Create Temp Table to hold Recods--Declare @HellTable TABLE (RecordID int IDENTITY (1,1) PRIMARY KEY,EmployeeID INT,[Record Date] DATETIME,EmployeeNumber INT,EmployeeName VARCHAR(30)-- ,DepartmentId INT,DepartmentName VARCHAR(30),Photo VARCHAR(30),SectionID INT,Designation VARCHAR(30),Arrival DATETIME, Departure DATETIME,-- StatusName VARCHAR(30),HoursWorked time(7)) -- Assign Date RangeIF @FROMDATE is NullSET @FROMDATE=CONVERT(VARCHAR(11),GETDATE())ElseSET @FROMDATE=CONVERT(VARCHAR(11),@FROMDATE)IF @TODATE is NULLSET @TODATE=CONVERT(VARCHAR(11),GETDATE())elseSET @TODATE=CONVERT(VARCHAR(11),@ToDate)-- Loop Though Date rangeWHILE @FROMDATE <= @TODATEBEGIN -- insert to Temp Table -- Check Holiday Set @holiday=dbo.fn_GETHOLIDAYS(@FromDate) if @holiday is Null SET @Holiday='Absent'Declare @hellTable TABLE (EmployeeID int, RecordDate datetime,employeeNumber int,EmployeeName varchar(30),DepartmentID int, DepartmentName varchar(30), Photo varchar(30), SectionID int,Designation varchar(50) ,Arrival DateTime,Departure DateTime,StatusName varchar(30),hoursWorked time(7))INSERT @hellTable(EmployeeID,RecordDate ,EmployeeNumber ,EmployeeName ,DepartmentID,DepartmentName,Photo,SectionID,Designation ,Arrival , Departure, StatusName ,HoursWorked ) SELECT e.employeeid, ISNULL(ar.RecordDate, @FROMDATE) AS 'Record Date', E.EmployeeNumber, e.EmployeeName, e.DepartmentID, d.DepartmentName, e.Photo, ISNULL(e.SectionID,0) AS SectionID, ISNULL(ds.JobTitle,'Unknown')as 'Designation', ar.Arrival, ar.Departure, ISNULL(s.StatusName,@Holiday) AS StatusName, ISNULL(ar.hoursworked,'00:00') AS 'HoursWorked'FROM Employees e LEFT OUTER JOIN EmployeeAttendanceRecords ar ON e.EmployeeID= ar.employeeid AND RecordDate = @FROMDATE OR RecordDate IS NULL LEFT JOIN AttendanceStatus s ON ISNULL(ar.AttendanceStatusID, 0) = s.StatusID JOIN Departments d on d.DepartmentID=e.DepartmentID JOIN Designations ds on ds.DesignationID=e.designationID WHERE e.AttendanceEnabled=1 SET @FROMDATE=DATEADD(day,1,@FROMDATE) END --Select Record SELECT EmployeeID,Recorddate As RecordDate ,EmployeeNumber ,EmployeeName ,DepartmentID,DepartmentName,Photo,SectionID,Designation ,Arrival , Departure, StatusName ,CONVERT(VARCHAR(5),Hoursworked, 108) as HoursWorked FROM @HellTable te Where (te.EmployeeID=COALESCE(@EmployeeID,te.EmployeeID) AND te.DepartmentID=COALESCE(@DepartmentID,te.DepartmentID) AND te.SectionID =COALESCE(@SectionID,te.SectionID) AND te.StatusName LIKE COALESCE(@Status + '%',te.StatusName)) ORDER BY RecordDate desc,DepartmentName,StatusName DescRashed |
|