I'm trying to get an average weight (stored in the quantity column of itemkey 'refr%') of the contents of a particular line item(TS:3050) and also the count of TS:3050. I have one table that lists credit memo header info and that joins to another table that shows transaction line details.
That part is easy enough.
The part I'm having difficulty with is I need to calculate the average weight of line item type TS:3050 ONLY when it's the only TS:# type on each invoice.
There are numerous other types of TS:# types, for instance TS:92125, TS:50, TS:240, etc. not to mention other non-related line item types, such as recov, other, disposal, etc.
So, if one particular invoice has TS:3050 AND TS:240, then I need to exclude that invoice from my calculation. If the invoice has only TS:3050 (and any other non TS: type line item) then it needs to be part of my calculation to find the average weight. The weight column is identified as "quantity".
So a valid credit memo that I want to calculate based off of might have this information:
An invoice I wouldn't want as part of my calculation would look something like this:
In the end, I would like an average number of TS:3050 and an average number of quantity of line item type 'Refr:%'.
Can someone give me a clue on how I might go about getting the results I'm looking for?
To get a list of all invoices within the date range that are listed on 2 possible CM templates, and that have the item key TS:3050 in them at all, I would do this:
I'm unclear about how I would ONLY use the creditmemos that ONLY have the TS type of TS:3050 in the invoice.
Any nudges in the right direction would be greatly appreciated.
That part is easy enough.
The part I'm having difficulty with is I need to calculate the average weight of line item type TS:3050 ONLY when it's the only TS:# type on each invoice.
There are numerous other types of TS:# types, for instance TS:92125, TS:50, TS:240, etc. not to mention other non-related line item types, such as recov, other, disposal, etc.
So, if one particular invoice has TS:3050 AND TS:240, then I need to exclude that invoice from my calculation. If the invoice has only TS:3050 (and any other non TS: type line item) then it needs to be part of my calculation to find the average weight. The weight column is identified as "quantity".
So a valid credit memo that I want to calculate based off of might have this information:
In the example above, I would use TS:3050's quantity of 1 as part of my average of the count of TS:3050 and I would use Refr:134's quantity of 4 to use as part of my average of 'Refr:%' quantity (I don't care what the number is after Refr. It could be Refr:22, Refr:12, Refr:410... it doesn't matter).Valid Credit Memo said:Invoice #1505
12/3/07
(Itemkey) (Description) (Quantity)
Location Description of location
TS:3050 TS 3050 done 1
Refr:134 4
An invoice I wouldn't want as part of my calculation would look something like this:
In the example above, because there's another TS:# type (TS:240) in addition to TS:3050, I can't use the quantity of TS:3050 in my calculation. I can ONLY use the invoices where there's ONLY TS type of TS:3050.Invalid Credit Memo said:Invoice #1504
12/2/07
(Itemkey) (Description) (Quantity)
Location Description of location
TS:3050 TS 3050 done 1
TS:240 TS 240 done 300
Refr:12 1,300
In the end, I would like an average number of TS:3050 and an average number of quantity of line item type 'Refr:%'.
Can someone give me a clue on how I might go about getting the results I'm looking for?
To get a list of all invoices within the date range that are listed on 2 possible CM templates, and that have the item key TS:3050 in them at all, I would do this:
Code:
SELECT CM.Refnumber, CM.franchise, CMLD.itemref_fullname
FROM CreditMemo CM JOIN CreditMemoLineDetail CMLD ON
CM.IDKey = CMLD.TxnID
WHERE (CM.txnDate >='1/1/2007' AND CM.txnDate <= '12/31/2008')
AND
(CM.templateref_fullname = 'CM-TS' OR CM.templateref_fullname = 'CM-WS')
AND
(itemref_fullname = 'TS:3050')
Any nudges in the right direction would be greatly appreciated.