SQL Problem

dusteater
09-22-2000, 03:31 PM
I have two tables the first one (Products) is a master list of products indexed by ProdId the second table(Loc1) is a list of products used by a location and only lists the ProdId. I want to sum the TotCost field of Loc1 but to break it down by criteria stored in Products (the criteria field is Category) I used tested the query and it works but I don't know how to access each part of the sum. ie. there are the folowing Categories...Meats, Produce, Other.
Here is my SQL Statement, but how do I access the totals for Meats, Produce...etc?
SQLProdSum = "SELECT SUM(Loc1.TotCost), Products.Category" _
& "From Products, Loc1" _
& "Where Products.ProdId = Loc1.ProdId" _
& "GROUP BY Products.Category" _
& "ORDER BY Products.Category"
rsProdSum.Open SQLProdSum, cnCasaFood, adOpenForwardOnly, adLockOptimistic
I know it must be simple but I can't figure it out
Thanks in advance!!
Rick

Q...
09-24-2000, 09:06 PM
Slight revision of you SQL Statement, firstly the inner join is always a good idea (better than a comma anyway) and returning the info in the order you would like is wise too:

SELECT Products.Category, Sum(Loc1.TotCost) AS TotalCost
FROM Products INNER JOIN Loc1 ON Products.ProdID=Loc1.ProdId
WHERE Products.ProdID=Loc1.ProdId
GROUP BY Products.Category
ORDER BY Products.Category;

With this statement you should end up with a recordset that contains as many records as categories. Each record having two fields Category and TotalCost.

You can then either place all the values into a multi dimensional array:

dim MyArray()

>recordset code<

redim MyArray(1,rx.RecordCount)

Do until x=rx.RecordCount
MyArray(0,X) = rx!Category
MyArray(1,X) = rx!TotalCost
X=X+1
Loop

of you could use the find method of the Rx to get the value you want:

Rx.MoveFirst
Rx.Find "Category='Meat'"
if Rx!Category = "Meat" then
AnotherVar = Rx!TotalCost
else
MsgBox "Not Found"
end if

Rx.MoveFirst
Rx.Find "Category='Other'"
etc.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum