TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
David Smith
NA
2k
0
Make Stored Procedure Cleaner and Faster
Dec 9 2015 2:10 PM
Can someone assist me with making working stored procedure faster and cleaner below
ALTER PROCEDURE [dbo].[ProcessCustomerHours]
@customerNumber varchar(50),
@startDate datetime,
@endDate datetime,
@firstBatch int,
@secondBatch int,
@hours decimal(6,2) output
AS
DECLARE @ResultValue decimal(6,2)
BEGIN
SELECT CustomerNumber
,SUM(CASE WHEN PayType = 1 THEN [Hours] ELSE 0 END) Hours
,SUM(CASE WHEN PayType <> 1 THEN [Hours] ELSE 0 END) PTO
,MIN(StartDate) StartDate
,MAX(EndDate) EndDate
,(Select Sum ([Hours]) PHours
FROM CustomerSheetHdr h
JOIN CustomerSheetDtl d on h.CustomerSheetHdrId=d.CustomerSheetHdrId
Where StartDate >= convert(datetime, @startDate)
And EndDate <= convert(datetime, @endDate)
And [CustomerNumber] = @customerNumber
And CustomerSheetBatchId in (@firstBatch, @secondBatch)
And PayType = 1
Group By CustomerNumber
Having Sum([Hours]) > 0) PreviousHours
INTO #TempAggregatedTable
FROM CustomerSheetHdr h
JOIN CustomerSheetDtl d on h.CustomerSheetHdrId=d.CustomerSheetHdrId
Where StartDate >= (Select Distinct MIN(StartDate)
FROM CustomerSheetHdr h
JOIN CustomerSheetDtl d on h.CustomerSheetHdrId=d.CustomerSheetHdrId
Where CustomerSheetBatchId in (@firstBatch, @secondBatch)
Group By CustomerNumber)
And EndDate <= (Select Distinct MAX(EndDate)
FROM CustomerSheetHdr h
JOIN CustomerSheetDtl d on h.CustomerSheetHdrId=d.CustomerSheetHdrId
Where CustomerSheetBatchId in (@firstBatch, @secondBatch)
Group By CustomerNumber)
And [CustomerNumber] = @customerNumber
And CustomerSheetBatchId in (@firstBatch, @secondBatch)
Group By CustomerNumber
Having Sum ([Hours]) > 80
Reply
Answers (
1
)
Sql Stored Procedure
SQL Stored Procedure If -Else Condition