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.
LVL 3
vbWhizAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
amitpagarwalConnect With a Mentor Commented:

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

Thanks


0
 
bbychkovCommented:
Try this:

SELECT
(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).
0
 
LKSIUNGCommented:
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)
FROM TABLE1
WHERE .......
GROUP BY TABLE1.Item_ID) p, TABLE2
WHERE p.Item_ID = TABLE2.Item_ID
0
 
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.

-vbWhiz
0
All Courses

From novice to tech pro — start learning today.