all groups > sql server mseq > august 2005 >
You're in the

sql server mseq

group:

TABLE Joins Problem


TABLE Joins Problem Jan S via SQLMonster.com
8/11/2005 12:00:00 AM
sql server mseq:
I have two sets of Data. For example Sales And Purchasing.

SALES(ISAL)
PURCHASING(IPUR)
-------------------------------
------------------------------------
ItemGroup
ItemGroup
ItemCode
ItemCode
DocDate
DocDate
Quantity
Quantity
Total
Total

The data mentioned above are all different with varying no. of rows in each

I need to do a join of the table just to show a simple summary by Item Group

For Example:

ItemGroup Total Sales Sold Qty Total Purchases
Purchased Qty
-------------- ------------------ ----------- -------
--------------- ---------------------
ACN 23432.90 234 1243.90
89
Etc.....



I am able to use the SUM Function for each table separately but when i join
the two, the values get all messed up. There is also a Master Table IGRP
which stores all the groups. There is also user variables
used to allow the user to specify the range of dates(but lets ignore that for
now).

What i tried:
SELECT T2.ItemGroup, SUM(T0.Total) as 'Total Purchases', SUM(T0.Quantity) as
'Purchased Qty', SUM(T1.Total) as 'Total Sales', SUM(T1.Quantity) as 'Sold
Qty' FROM IPUR T0 FULL OUTER JOIN ISAL T1 ON V1.ItemCode = V0.ItemCode INNER
JOIN IGRP ON T2.ItemGroup = T0.ItemGroup OR T2.ItemGroup = T1.ItemGroup GROUP
BY T3.ItemGroup

The resultset returned is correct for some entries but 2-5x higher for most.

Please help...Is there a way or do i just have to seperate both of them?


--
Message posted via SQLMonster.com
Re: TABLE Joins Problem Jan S via SQLMonster.com
8/11/2005 12:00:00 AM
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.....


--
Message posted via SQLMonster.com
Re: TABLE Joins Problem Hugo Kornelis
8/11/2005 9:18:16 PM
[quoted text, click to view]

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
--

AddThis Social Bookmark Button