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