I'm trying to join data from two table A, and B on a timestamp. I ideally want every row of tableA that meet the unitCode and time constraints, and if there is an associated row in tableB then join it, otherwise assume the coloumns from table B are attached but are null or empty.
Assume TableA looks something like this:
id | timestamp | itemA1 | itemA2 | itemA3
and TableB looks somthing like this:
id | timestamp | itemB1 | itemB2
so data might look like this:
- timestamp | itemA1 | itemA2 | itemA3 | itemB1 | itemB2
- [datetime]| 10 | 11 | 40 | 26 | 12
- [datetime]| 10 | 11 | 40 | 26 | 12
- [datetime]| 10 | 11 | 40 | null | null
- [datetime]| 10 | 11 | 40 | 26 | 12
- [datetime]| 10 | 11 | 40 | null | null
Currently I'm using this:
SELECT * FROM tableA AS a JOIN (SELECT * FROM tableB) AS b ON (a.timestamp = b.timestamp) WHERE b.unitCode = 'CODE' AND b.timestamp BETWEEN 'STARTDATETIME' AND 'ENDDATETIME' ORDER BY b.timestamp DESC`
but it only makes rows out of the instances where there is a corresponding tableB for a given tableA.