Table Join with aggregate functions:

I have 2 tables. Table 1 contains 5 fields significant to this query:

Item_ID, Store_ID, Qty, Amt, Business_Date

Table 2 contains 2 fields significant to this query

Item_ID, Description

I want to perform an aggregate query on Table1 For a specific business_date and for a list of store_id s, summarizing by Item_ID and listing the total Qty and the total extended (Qty * Amt)

This is no problem, however, I also want to join this aggregation to Table2 to also have the Item Description. Usually this is no problem except in cases where the list of items for one store is different than the rest of the stores in this case my query hangs (until I mercifully kill it). In cases where all the items match the query runs extremely fast (less than 1 second)

Here is my current SQL:

SELECT TABLE2.description, SUM(TABLE1.qty * TABLE1.amt) AS total_extended, SUM(TABLE1.qty) AS total_items FROM TABLE1 INNER JOIN TABLE2 ON TABLE1.item_ID = TABLE2.item_ID WHERE (TABLE1.store_ID = '002434' Or TABLE1.store_ID = '002606' Or TABLE1.store_ID = '002870') AND (TABLE1.business_date = '1/21/00') GROUP BY TABLE1.Item_ID, TABLE2.description, ORDER BY TABLE2.description

Is there a way to structure the SQL to run the aggregation before the Join? Really table 2 has no business being part of the aggregation anyway, but I do want it included in the results.

I can provide more info or clarity if needed.

Thanks for your help in advance.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Try this:

(select TABLE2.description from table2
where table2.item_ID=table1.item_ID) AS Description, SUM(TABLE1.qty * TABLE1.amt) AS total_extended, SUM(TABLE1.qty) AS total_items FROM TABLE1 WHERE (TABLE1.store_ID = '002434' Or TABLE1.store_ID = '002606' Or TABLE1.store_ID = '002870') AND (TABLE1.business_date = '1/21/00') GROUP BY TABLE1.Item_ID ORDER BY Description

(I delete Group By TABLE2.Description).
Try this, I think this one is faster:

SELECT p.Item_ID, TABLE2.description  ,p.total_items, p.total_extended
FROM (SELECT TABLE1.Item_ID, total_items=SUM(Qty),
total_extended=SUM(Qty * Amt)
WHERE .......
WHERE p.Item_ID = TABLE2.Item_ID

Hope the following SQL solves your purpose :

select T2.*, sum(T1.qty*T1.amt) ..
from table1 T1, table2 T2
where T1.itemid = T2.itemid
and .....
group by T2.itemid, T2.descr

Please contact if any more problem arises



Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
vbWhizAuthor Commented:
Thanks all for your comments...

I'll post points for all of you..

The most optimum was actually amitpagarwal's answer.. for some reason the join was causing problems... By performing the join outside the FROM (in the where) the query runs just fine..

All your answers worked great though

thank you for your help.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.