views:

21

answers:

3

I have a table 'TableA':

TableA_ID (int) Date (smalldatetime) Name (string)

and a table 'TableB' linked with TableA by TableA_ID: TableB_ID (int) TableA_ID (int) Description (string) Total (double)

I want to sum up the TableB 'Total' column between two dates (TableA 'Date').

+3  A: 

Hi,

This should work:

SELECT SUM(b.total)
FROM tableB AS b
INNER JOIN tableA AS a ON a.tableA_ID = b.tableA_ID
WHERE a.date BETWEEN <start_date> AND <end_date>

Regards, Alin

Alin Purcaru
Give or take trivia in the formatting, the same as mine...maybe one of us is about correct! :D
Jonathan Leffler
I think @Alin Purcaru and @Jonathan Leffler have great answers :)
bobs
presentation is everything ;)
Alin Purcaru
Thank you Alin!
milo2010
+2  A: 

What about:

SELECT SUM(B.Total)
  FROM TableB AS B JOIN TableA AS A ON B.TableA_ID = A.TableA_ID
 WHERE A.Date BETWEEN '<early-date>' AND '<later-date>';
Jonathan Leffler
Thank you! It worked!
milo2010
+1  A: 

This might help.

SELECT SUM(Total)
FROM TableA
JOIN TableB ON TableA.TableA_ID = TableB.TableA_ID
WHERE TableA.Date BETWEEN 'start date' AND 'end date'
bobs
Thank you bobs!
milo2010