Greetings,I have two tables Transaction and TransactionDetail. For each row in transaction, the TransactionDetail table has many rows. This is how the tables look likeTransaction TableTransactionID ProductID TransactionDate~~~~~~~~~~~~~ ~~~~~~~~~ ~~~~~~~~~~~~~~~~ 123456789 ABCXYZ 10/09/2009TransactionDetail tableTransactionDetailID TransactionID Amount~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~ ~~~~~~ 1 123456789 25 2 123456789 50 3 123456789 15
I need to write a query that will sum the amounts for each transaction. Both Transaction and TransactionDetail have a million rows or more on them.I wrote this query.SELECT trans.ProductID ProductID, trans.TransactionDate Date ( select SUM(Amount) from TransactionDetail tdetl where tdetl.TransactionID = trans.TransactionID ) Total,FROM [Transaction] transWHERE AND trans.TransactionDate = '10/02/2009'
This however is very slow and take like 10 minutes to complete. Where have I gone wrong? Also note that this is a simplified version of what it really is. There are multiple SUM columns.Can someone help?