Problem With Where Claus

Feb 21, 2008

I have a rather long and ugly query that joins seven or eight tables, does some aggreagaions and a union.

It takes about a minute to return 300,000 rows. When I add a where condition which uses a non-key integer field it pares the processing down to 20 seconds to get 2,000 rows. However if I add a second non-key integer field to my where claus it takes just under 30 MINUTES to produce a similar result set.

This is so far out of whak that i don't even know where to start to look.

Here is the query (and I just got this this morning so I haven't worked on it to tune it - I already see a few areas I want to change):

SELECT AMTRANHDR.SMBNKNumber, AMTRANHDR.AMACTNumber, AMTRANHDR.AMALTNumber,
POORDERHDR.POORHNumber, AMTRANHDR.AMTRHNumber, AMTRANHDR.AMTRHType, AMTRANHDR.AMTRHSubType,
AMTRANHDR.AMTRHCode, AMTRANHDR.AMTRHDesc, AMTRANHDR.AMTRHDate, AMTRANHDR.AMTRHAmt, AMTRANHDR.AMTRHDueDate,
AMTRANHDR.AMTRHDiscAllowed, AMTRANHDR.AMTRHDiscDate, AMTRANHDR.SMBCHNumber, AMTRANHDR.AMTRHMasterCode,
isnull(TotalBalance.BalDue,0) BalDue, isnull(TotalAmtNoDiscAdj.PaidAmt,0) as PaidAmt,
isnull(TotalDisc.DiscTaken,0) as DiscTaken, isnull(TotalAdj.AdjTaken,0) as AdjTaken,
AMTRANHDR.AMTRHRecvShip, CASE WHEN AMTRANHDR.AMTRHStatus = 'P' THEN 'Paid' WHEN AMTRANHDR.AMTRHStatus = ' ' THEN 'Open'
WHEN AMTRANHDR.AMTRHStatus = 'S' THEN 'Select' WHEN AMTRANHDR.AMTRHStatus = 'H' THEN 'Hold' WHEN AMTRANHDR.AMTRHStatus = 'C' THEN 'Conversion' end as AMTRHStatus,
AMTRANHDR.POORRNumber, POORDERREL.POORDNumber, POORDERDTL.POORHNumber, POORDERHDR.POORHCode, POORDERHDR.POORHCode + '-'
+ POORDERDTL.POORDSeq + '-' + POORDERREL.POORRSeq AS ReleaseNumber, Payments.LastPaymentDate,
Payments.PaymentCount, SMCODEBCH.SMBCHStatus, AMALTERNATE.AMALTCode, AMALTERNATE.AMALTName
FROM [dbo].AMTRANHDR
LEFT OUTER JOIN [dbo].AMALTERNATE ON AMTRANHDR.AMALTNumber = AMALTERNATE.AMALTNumber
LEFT OUTER JOIN [dbo].SMCODEBCH ON AMTRANHDR.SMBCHNumber = SMCODEBCH.SMBCHNumber
LEFT OUTER JOIN [dbo].POORDERREL ON AMTRANHDR.POORRNumber = POORDERREL.POORRNumber
LEFT OUTER JOIN [dbo].POORDERDTL ON POORDERREL.POORDNumber = POORDERDTL.POORDNumber
LEFT OUTER JOIN [dbo].POORDERHDR ON POORDERDTL.POORHNumber = POORDERHDR.POORHNumber
LEFT OUTER JOIN [dbo].ICINVENTORY ON AMTRANHDR.AMTRHNumber = ICINVENTORY.AMTRHNumber
LEFT OUTER JOIN (select amtrhnumber, 0 as BalDue from AMTRANHDR
where AMTRHType = 'P'
UNION select AMTRANHDR.AMTRHNumber, AMTRHAmt - ISNULL(PaidTran.BalDue,0) as BalDue FROM AMTRANHDR LEFT OUTER JOIN
(SELECT AMPMTCROSS.AMPMCItem, sum(isnull(AMPMTCROSS.AMPMCAmount,0) +
isnull(AMPMTCROSS.AMPMCDiscount,0) + isnull(AMPMTCROSS.AMPMCAdjust,0)) as BalDue
FROM [dbo].AMPMTCROSS GROUP BY AMPMTCROSS.AMPMCItem) as PaidTran ON PaidTran.AMPMCItem = AMTRANHDR.AMTRHNumber
where AMTRHType = 'T')
AS TotalBalance ON AMTRANHDR.AMTRHNumber = TotalBalance.Amtrhnumber
LEFT OUTER JOIN (SELECT AMPMTCROSS.AMPMCItem, sum(isnull(AMPMTCROSS.AMPMCAmount,0)) as PaidAmt
FROM [dbo].AMPMTCROSS
INNER JOIN [dbo].AMTRANHDR ON AMPMTCROSS.AMPMCCheck = AMTRANHDR.AMTRHNumber
GROUP BY AMPMTCROSS.AMPMCItem) AS TotalAmtNoDiscAdj ON TotalAmtNoDiscAdj.AMPMCItem = AMTRANHDR.AMTRHNumber
LEFT OUTER JOIN (SELECT Count(0) AS PaymentCount, AMPMCItem, MAX(AMTRHDate) AS LastPaymentDate
FROM [dbo].AMPMTCROSS
INNER JOIN [dbo].AMTRANHDR ON AMPMTCROSS.AMPMCCheck = AMTRANHDR.AMTRHNumber
GROUP BY AMPMTCROSS.AMPMCItem) AS Payments ON AMTRANHDR.AMTRHNumber = Payments.AMPMCItem
LEFT OUTER JOIN (SELECT AMPMTCROSS.AMPMCItem, sum(isnull(AMPMTCROSS.AMPMCDiscount,0)) as DiscTaken
FROM [dbo].AMPMTCROSS
INNER JOIN [dbo].AMTRANHDR ON AMPMTCROSS.AMPMCCheck = AMTRANHDR.AMTRHNumber
GROUP BY AMPMTCROSS.AMPMCItem) AS TotalDisc ON AMTRANHDR.AMTRHNumber = TotalDisc.AMPMCItem
LEFT OUTER JOIN (SELECT AMPMTCROSS.AMPMCItem, sum(isnull(AMPMTCROSS.AMPMCAdjust,0)) as AdjTaken
FROM [dbo].AMPMTCROSS
INNER JOIN [dbo].AMTRANHDR ON AMPMTCROSS.AMPMCCheck = AMTRANHDR.AMTRHNumber
GROUP BY AMPMTCROSS.AMPMCItem) AS TotalAdj ON AMTRANHDR.AMTRHNumber = TotalAdj.AMPMCItem
where AMTRANHDR.AMTRHDeletedOn is null and AMTRANHDR.ADDIVNumber = 8
and AMTRANHDR.AMACTNumber = 21179


and AMTRANHDR.AMALTNumber = 23195 --THIS IS WHERE I RUN INTO TROUBLE (either alone take 20 seconds, together 30 minutes)

View 6 Replies


ADVERTISEMENT

Using IN Claus In The Query

Apr 1, 2005

Hi,
I need to pass a lsit of items to a query which will be used using IN clauslike campus_code IN (@CampusCodes) .  IF the @CampusCodes is "SD34, FHT34, JK678", the SQL server is not returning any rows back.
If the @CampusCodes is only  "SD34", the SQL server is returning rows back as it suould be.
How can fix it so that i can pass more than one value in CampusCodes variable?
Thanks

View 4 Replies View Related

Case Expression In Where Claus

Jan 11, 2006

I there a way to differ the filtering field through a Case expression in the Where claus?
ex:
Where   Case    When @StuNum = ''    Then S.SSN = @SSN   Else     S.StuNum = @StuNum  End
And ...
The actual field to filter by differs, but I can't seem to find the right syntax to do this.
Any help is appreciated : )

View 3 Replies View Related

Aggregate Where Claus Problem =/

Dec 4, 2006

Hi Guys,

Is it possible to have a where clause (or other method) where you only select the max value from this: SUM(ORDER_ITEM.ItemQuantity) and only output that 1 row... or even perhaps a range of rows... in others words... find the ItemID with the greatest combined Quantity

Heres the query so far:

SELECT ORDER_ITEM.ItemID, SUM(ORDER_ITEM.ItemQuantity) AS 'Max'
FROM ORDER_ITEM
GROUP BY ORDER_ITEM.ItemID

Thx for reading :-)

--Philkills

View 5 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved