× The internal search function is temporarily non-functional. The current search engine is no longer viable and we are researching alternatives.
As a stop gap measure, we are using Google's custom search engine service.
If you know of an easy to use, open source, search engine ... please contact support@midrange.com.



I have a complex SQL statement that is taking a very long time to execute.
This statement takes about 30 - 45 seconds, and is executed about 700 times over the duration of the program.

What I am attempting to do is to determine if I have any unmatched transactions from my invoice detail file to a customer supplied invoice payment file.
What makes it complicated is that the same customer item number can refer to two different items in my invoice file based on whether the purchased quantity or donated quantity was used.
The customer supplies me with an invoice number, their item number, the purchased qty and the donated quantity for each item on the invoice.  In addition, they will summarize the quantity if the item appears multiple times on the invoice or if there is a credit with the same invoice number.

The files that I am using are:
VASTRAND - Customer transactions
Invoice
Customer item
:Purchase Qty
Donated Qty

For file VASTRAND, there is an index over the customer item number field.
In addition, I have a separate view for purchased items and for donated items.

VSADETL - Invoice Details
Invoice
Customer Item
Invoice Type
Customer Item Numer
Our Item Number

FINITEM - Item Master
Our Item Number
Donated Flag

If the customer transaction has donated quantity, it must match a donated item for the same customer item on my invoice.  If it is purchased item, it must match a purchased item on the invoice.

The SQL I am using is:
With Purchased as (    SELECT Distinct ASCSIT FROM VAsTranD
Where     AsPQty <> 0 and AsInv = ?), Donated as(    SELECT
Distinct ASCSIT FROM VAsTranD Where     AsDQty <> 0 and AsInv = ?)     Select SbItem,SbCsIt,    Sum(Case SbType When 'O'
 Then SbQShp Else (SbQShp * -1) End) SbQShp,    Min(SbPric)
SbPrice,Min(FIcDonated) FIcDonated from VSADETL     Join FInItem on SBItem = FICItem Where     ((FIcDonated = 'Y' and SbCsIt Not In (Select AsCsIt From Donated))     Or (FIcDonated
 = 'N' and SbCsIt Not In (Select AsCsIt From Purchased)))  
  and SbDel = 'A'and Sbinv = ?     Group by SbItem,SbCsIt  
 Union All     Select SbItem,SbCsIt,    Sum(Case SbType When
 'O' Then SbQShp Else (SbQShp * -1) End) SbQShp,    Min(SbPric) SbPrice,Min(FIcDonated) FIcDonated from VSADETL     Join
 FInItem on SbItem = FIcItem     Exception Join VAsTranD on
SbInv = AsInv and SbCsIt = AsCsIt     Where SbDel = 'A' and
SbInv = ?     Group by SbCSit,SbItem    Order by SbItem,SbCsIt                                                         
 
Any suggestions for improving this will be appreciated.

Jeff Young
Sr. Programmer Analyst
IBM -e(logo) server Certified Systems Exper - iSeries Technical Solutions V5R2 
IBM  Certified Specialist- e(logo) server i5Series Technical Solutions Designer V5R3
IBM  Certified Specialist- e(logo)server i5Series Technical Solutions Implementer V5R3

As an Amazon Associate we earn from qualifying purchases.

This thread ...

Follow-Ups:

Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.