B
Brady Mann - Amatuer Extraordinaire
Gang,
I've read through many of the postings on this site and I'm getting
close (but have no cigar). Here is what I have in a single table (data
changed to keep it simple)
ID VendorName Parts QtyOrdered AvgPartCost
1 Fred ABC 1 $.05
2 Fred ABC 1 $.05
3 Fred ABB 2 $.05
4 Bob XYZ 2 $.05
5 Bob XYY 2 $.05
This is what I want from a Query:
VendorName Parts(Unique) TotalSpend
(QtyOrdered*AvgPartCost)
Fred 2 $.20
Bob 2 $.20
As you can see above, I want to determine the number of UNIQUE parts
purchased by VendorName. However, I want to SUM the total of all
purchases and group them by VendorName.
Thus far, I've been able to find the total unique parts by VendorName
with this SQL (note, the below is rather sloppy and I'm sure there is a
cleaner method):
***startSQL
SELECT [%$##@_Alias].VendorName, Count(*) AS Parts
FROM [SELECT DISTINCT
DatabaseQuery.VendorName,
DatabaseQuery.Parts
FROM
DatabaseQuery]. AS [%$##@_Alias]
GROUP BY [%$##@_Alias].VendorName;
***end SQL
I can get the total spend per supplier like this:
***start SQL
SELECT DatabaseQuery.VendorName, Sum(OrderedQty*AvgPartCost) AS
["TotalPurchase"]
FROM DatabaseQuery
GROUP BY DatabaseQuery.VendorName;
***end SQL
What I can't seem to do is get this all into one query. I've tried
using the UNION statement but...when I do that I get a TOTAL part count
instead of a unique part count (in my example above, the total parts
for "Fred" would be 3 instead of 2).
How can I put this calmly...HELP!!!!!!!!!!!!!!!!
I've read through many of the postings on this site and I'm getting
close (but have no cigar). Here is what I have in a single table (data
changed to keep it simple)
ID VendorName Parts QtyOrdered AvgPartCost
1 Fred ABC 1 $.05
2 Fred ABC 1 $.05
3 Fred ABB 2 $.05
4 Bob XYZ 2 $.05
5 Bob XYY 2 $.05
This is what I want from a Query:
VendorName Parts(Unique) TotalSpend
(QtyOrdered*AvgPartCost)
Fred 2 $.20
Bob 2 $.20
As you can see above, I want to determine the number of UNIQUE parts
purchased by VendorName. However, I want to SUM the total of all
purchases and group them by VendorName.
Thus far, I've been able to find the total unique parts by VendorName
with this SQL (note, the below is rather sloppy and I'm sure there is a
cleaner method):
***startSQL
SELECT [%$##@_Alias].VendorName, Count(*) AS Parts
FROM [SELECT DISTINCT
DatabaseQuery.VendorName,
DatabaseQuery.Parts
FROM
DatabaseQuery]. AS [%$##@_Alias]
GROUP BY [%$##@_Alias].VendorName;
***end SQL
I can get the total spend per supplier like this:
***start SQL
SELECT DatabaseQuery.VendorName, Sum(OrderedQty*AvgPartCost) AS
["TotalPurchase"]
FROM DatabaseQuery
GROUP BY DatabaseQuery.VendorName;
***end SQL
What I can't seem to do is get this all into one query. I've tried
using the UNION statement but...when I do that I get a TOTAL part count
instead of a unique part count (in my example above, the total parts
for "Fred" would be 3 instead of 2).
How can I put this calmly...HELP!!!!!!!!!!!!!!!!