I'm trying to join 2 tables, where all data in the left table appears and only data that matches in the right table appears - should be a simple LEFT JOIN. But I either get results where only data in both tables appear if they match, or some of the data in left table and a correct set of data that matches in the right table.
Here's the code:
SELECT
tblCalendar.dDate, tblCalendar.dDay,
tblDailyData.TradeableDate, tblDailyData.TradeableCode
FROM
tblCalendar
LEFT JOIN
tblDailyData ON tblCalendar.[dDate] = tblDailyData.[TradeableDate]
WHERE
(((tblDailyData.TradeableCode) = "MSFT"))
OR (((tblDailyData.TradeableDate) IS NULL)
AND ((tblDailyData.TradeableCode) IS NULL))
ORDER BY
tblCalendar.dDate;
Correct output should be (see image):
- all
tblCalendar.dDateandtblCalendar.dDayrows - and only the rows where
tblDailyData.TradeableDateandtblDailyData.TradeableCodecorrespond to the tblCalendar days.
I've been trying to figure this out for hours. I've tried ChatGPT, resources here, numerous online resources, and the query builder in MS Access. I suspect I'm doing something wrong with the WHERE and ON clauses, but I can't figure out what.
Appreciate any help the group could provide.
Without seeing the data, it's hard to determine the core issue. These two lines, would only join if you had a
NULLintblCalendar.dDate. A simple test in your query would be to do aFULL OUTER JOINinstead of aLEFT JOIN.If it's necessary to get
NULLtblDailyData.TradeableDate, you could handle it with aRIGHT JOINtotblCalendaras a set, thenFULL OUTTER JOINfromtblCalendarto that set.