Quantcast
Channel: Answers for "JOIN or Case When possibilities"
Viewing all articles
Browse latest Browse all 6

Answer by WilliamD

$
0
0
OK, so I took some time off this weekend - otherwise you would have had an answer earlier. As far as I understand it the problem has two solutions that spring to mind, the first being to change the function supplied by @djpbusinessinc into a table valued function: CREATE FUNCTION dbo.GetAppropriateEmployeeId (@SalesEmpId int, @AccountManagerEmpId int) RETURNS TABLE AS RETURN SELECT CASE WHEN (FirstName NOT LIKE '(SR%)' OR FirstName LIKE '(SR 00%)' OR FirstName LIKE '(SR 99%)') AND @SalesEmpId @AccountManagerEmpId THEN @SalesEmpId ELSE @AccountManagerEmpId END AS EmpNid FROM dbo.Employees E WHERE EmpNid = @SalesEmpId GO DECLARE @FromDate AS date, @ThruDate AS date, @Nid AS int SET @FromDate = '2011-01-24' SET @ThruDate = '2011-01-29' SELECT DISTINCT O.TicketNumber, I.RecName, I.CommissionPct, L.QtyShipped, E.RecName AS 'SALESREP', C.AcctManagerNid, O.SlsEmpNid, C.RecName AS 'CUSTOMER' FROM dbo.Orders AS O JOIN dbo.OrderLines AS L ON O.TicketNumber = L.OrderTicketNumber JOIN dbo.Items AS I ON L.ItemNid = I.ItemNid JOIN dbo.CusPaymentApplies AS CP ON O.TicketNumber = CP.ToOrderTicketNumber JOIN dbo.Customers AS C ON C.CusNid = O.ToCusNid CROSS APPLY dbo.GetAppropriateEmployeeId(o.SlsEmpNid, c.AcctManagerNid) GAEI INNER JOIN dbo.Employees E ON GAEI.EmpNid = E.EmpNid WHERE O.TotalReceivableRemaining = 0 AND CP.BatchEntryDate BETWEEN @FromDate AND @ThruDate AND I.ProductClassNid IN (1, 2) AND C.ActiveFlag = 1 AND (O.ReceivablesNote NOT LIKE 'NC%' OR O.ReceivablesNote IS NULL) AND O.isvoided = 0 ORDER BY SALESREP, TicketNumber, RecName I then rewrote the code to use two left joins on `Employee`, joining on either Sales Person or Account Manager, then doing the CASE comparison in the output. DECLARE @FromDate AS date, @ThruDate AS date, @Nid AS int SET @FromDate = '2011-01-24' SET @ThruDate = '2011-01-29' SELECT DISTINCT O.TicketNumber, I.RecName, I.CommissionPct, L.QtyShipped, CASE WHEN AMGR.EmpNid SLS.EmpNid AND (AMGR.FirstName NOT LIKE '%SR%' OR AMGR.FirstName LIKE '(SR 00%' OR AMGR.FirstName LIKE '(SR 99%') THEN AMGR.RecName ELSE SLS.RecName END AS 'SALESREP', C.AcctManagerNid, O.SlsEmpNid, C.RecName AS 'CUSTOMER' FROM dbo.Orders AS O JOIN dbo.OrderLines AS L ON O.TicketNumber = L.OrderTicketNumber JOIN dbo.Items AS I ON L.ItemNid = I.ItemNid JOIN dbo.CusPaymentApplies AS CP ON O.TicketNumber = CP.ToOrderTicketNumber JOIN dbo.Customers AS C ON C.CusNid = O.ToCusNid LEFT JOIN dbo.Employees AS AMGR ON AMGR.EmpNid = C.AcctManagerNid LEFT JOIN dbo.Employees AS SLS ON SLS.EmpNid = O.SlsEmpNid WHERE O.TotalReceivableRemaining = 0 AND CP.BatchEntryDate BETWEEN @FromDate AND @ThruDate AND I.ProductClassNid IN (1, 2) AND C.ActiveFlag = 1 AND (O.ReceivablesNote NOT LIKE 'NC%' OR O.ReceivablesNote IS NULL) AND O.isvoided = 0 ORDER BY SALESREP, TicketNumber, RecName ; Both solutions seem to return the correct results (you have to check that out), but the second solution (without a function) was faster on my test box. Your mileage may vary there, so test both out with a representative data set. Let us know if that solves the problem.

Viewing all articles
Browse latest Browse all 6

Latest Images

Trending Articles





Latest Images