I have a complex query in Access 2010 where:
query1 is a query WHERE field1 <> 8
and
query2 is the same query WHERE field1=8.
Then I am doing:
SELECT * FROM query1
INNER JOIN query2 ON query1.field2=query2.field2
WHERE query1.field3=query2.field3
This returns 0 results.
However when I change it to:
SELECT * from query1
INNER JOIN query2 ON query1.field2=query2.field2
WHERE query1.field3=5 AND query2.field3=5
I do get results. Does this make sense to anybody? Could it have to do with on of the fields being considered a text field? except it works without quotes so I don’t know why that would be.
Please let me know if you have seen this before.
This is the full query:
SELECT *
FROM (SELECT [transactions by category].[categoryid],
Month([account transactions].[transaction date]) AS TransMonth,
Year([account transactions].[transaction date]) AS TransYear,
SUM([transactions by category].[amount]) AS Amount
FROM (categories
INNER JOIN [transactions by category]
ON categories.id =
[transactions by category].categoryid)
INNER JOIN [account transactions]
ON [transactions by category].transactionid =
[account transactions].id
WHERE [account transactions].[transaction type] <> 8
GROUP BY [transactions by category].[categoryid],
Year([account transactions].[transaction date]),
Month([account transactions].[transaction date])) AS
TransactionCredits
INNER JOIN (SELECT [transactions by category].[categoryid],
Month([account transactions].[transaction date]) AS
TransMonth,
Year([account transactions].[transaction date]) AS
TransYear
,
SUM(
[transactions by category].[amount])
FROM (categories
INNER JOIN [transactions by category]
ON categories.id =
[transactions by category].categoryid)
INNER JOIN [account transactions]
ON [transactions by category].transactionid =
[account transactions].id
WHERE [account transactions].[transaction type] = 8
GROUP BY [transactions by category].[categoryid],
Year([account transactions].[transaction date]),
Month([account transactions].[transaction date]))
AS
TransactionDebits
ON TransactionCredits.[categoryid] =
TransactionDebits.[categoryid]
WHERE TransactionCredits.transyear = TransactionDebits.transyear
AND TransactionCredits.transmonth = 8
AND TransactionDebits.transmonth = 8;
So, I’ve been banging my head against the wall for months on this. I finally found the solution, but I don’t fully understand why this works.
Instead of just saying
I changed the WHERE clause to say
Both of these fields were originally integer ID columns but apparently one of them became a text field of some sort somewhere in one of the queries.
I would still be interested in hearing an explanation of how this works.
Thanks