Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sum fields from two different tables

Status
Not open for further replies.

razum

Programmer
Sep 6, 2006
10
US
Hi all.
i have 2 tables t1 and t2 and each one has field price and date, i'd like to sum price on both tables and group it by date.
tnx everyone
 
Code:
SELECT DateField, SUM(Price) AS Price
       FROM (SELECT DateField, Price FROM Table1
             UNION ALL
             SELECT DateField, Price FROM Table2) Tbl1
GROUP BY DateField

--- or:
SELECT COALESCE(Table1.DateField, Table2.DateField) AS DateField,
SUM(ISNULL(Table1.Price,0)+ISNULL(Table2.Price,0)) AS Price
FROM Table1
FULL JOIN Table2 ON Table1.DateField = Table2.DateField
GROUP BY COALESCE(Table1.DateField, Table2.DateField)


Borislav Borissov
VFP9 SP1, SQL Server 2000/2005.
MVP VFP
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top