[quoted text, click to view] On Thu, 11 Aug 2005 02:47:08 GMT, Jan S via SQLMonster.com wrote:
>Sorry abt the crappy illustrations...Here it is again.
>SALES(ISAL) PURCHASING(IPUR)
>--------------------- -----------------------------
>ItemGroup ItemGroup
>ItemCode ItemCode
>DocDate DocDate
>Quantity Quantity
>Total Total
>
>
>What i need:
> ItemGroup Total Sales Sold Qty Total Purchases Purchased
>Qty
>----------------- ---------------- ----------- ------------------
>---- ---------------------
> ACN 23432.90 234 1243.90
>89
>Etc.....
Hi Jan,
The best way to post your table structure and sample data as to use
CREATE TABLE and INSERT statements - that also has the added advantage
of providing easy to copy-and-paste test data! See
www.aspfaq.com/5006 for more info on the best way to ask for help in these groups.
Anyway, here are two untested queries that will probably both return the
results you need. Use the one you like best, or use them as a basis for
your own version.
SELECT s.ItemGroup,
SUM(s.Total) AS TotalSales,
SUM(s.Quantity) AS SoldQty,
(SELECT SUM(p.Total)
FROM Purchases AS p
WHERE p.ItemGroup = s.ItemGroup) AS TotalPurchases,
(SELECT SUM(p.Quantity)
FROM Purchases AS p
WHERE p.ItemGroup = s.ItemGroup) AS PurchasedQty
FROM Sales AS s
GROUP BY s.ItemGroup
or
SELECT s.ItemGroup,
s.TotalSales, s.SoldQty,
p.TotalPurchases, p,PurchasedQty
FROM (SELECT ItemGroup, SUM(Total), SUM(Quantity)
FROM Sales
GROUP BY ItemGroup) AS s(ItemGroup, TotalSales, SoldQty)
INNER JOIN (SELECT ItemGroup, SUM(Total), SUM(Quantity)
FROM Purchases
GROUP BY ItemGroup) AS p(ItemGroup, TotalPurchases,
PurchasedQty)
ON p.ItemGroup = s.ItemGroup
Best, Hugo
--