I am using an MSAccess 2007 database file to retrive some info
The tables look like this
tblIncome
Date | Takings
12/05/2007 | 10
12/05/2007 | 20
13/05/2007 | 10
16/05/2007 | 40
tblExpence
Date | Takings
12/05/2007 | 10
13/05/2007 | 20
14/05/2007 | 50
When running the code :
in tblExpence the Date 12/05/2007 returns the 10 twice due to the join
is there a way to stop it from returning it twice so the answer for the 12/05/2007 would be 30 in and 10 out
Dim OLEStr As String
OLEStr = "SELECT [Date], SUM(Income) AS Income, SUM(Expence) AS Expence, SUM(Total) AS Total FROM ("
OLEStr += " SELECT"
OLEStr += " Year(tblIncome.Date1) AS [Date],"
OLEStr += " SUM(tblIncome.Takings) AS Income,"
OLEStr += " SUM(tblExpences.Total) AS Expence,"
OLEStr += " (Income - Expence) AS Total"
OLEStr += " FROM"
OLEStr += " tblIncome INNER JOIN tblExpences ON tblIncome.Date1 = tblExpences.Date1"
OLEStr += " WHERE"
OLEStr += " Year(tblIncome.Date1) = " & Year
OLEStr += " GROUP BY tblIncome.Date1"
OLEStr += " UNION ALL"
OLEStr += " SELECT"
OLEStr += " Year(tblIncome.Date1) AS [Date],"
OLEStr += " SUM(tblIncome.Takings) AS Income,"
OLEStr += " SUM(tblExpences.Total) AS Expence,"
OLEStr += " Income AS Total"
OLEStr += " FROM"
OLEStr += " tblIncome LEFT JOIN tblExpences ON tblIncome.Date1 = tblExpences.Date1"
OLEStr += " WHERE"
OLEStr += " Year(tblIncome.Date1) = " & Year
OLEStr += " AND"
OLEStr += " tblExpences.Total IS NULL"
OLEStr += " GROUP BY tblIncome.Date1"
OLEStr += " UNION ALL"
OLEStr += " SELECT"
OLEStr += " Year(tblExpences.Date1) AS [Date],"
OLEStr += " SUM(tblIncome.Takings) AS Income,"
OLEStr += " SUM(tblExpences.Total) AS Expence,"
OLEStr += " Expence AS Total"
OLEStr += " FROM"
OLEStr += " tblIncome RIGHT JOIN tblExpences ON tblIncome.Date1 = tblExpences.Date1"
OLEStr += " WHERE"
OLEStr += " Year(tblExpences.Date1) = " & Year
OLEStr += " AND"
OLEStr += " tblIncome.Takings IS NULL"
OLEStr += " GROUP BY tblExpences.Date1"
OLEStr += ")"
OLEStr += " GROUP BY [Date]"