Access 2010 WHERE clause not working as expected - where-clause

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
SELECT *
FROM query1
INNER JOIN query2 ON query1.field2=query2.field2
WHERE query1.field3=query2.field3"
I changed the WHERE clause to say
VAL(query1.field3)=VAL(query2.field3)
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

Related

How can add "INNER JOIN" to an existing "NOT EXIST" Query?. The query is in the description below

I have an existing Ms Access query which works just fine as per my need, now I want to join another two tables ("Taxes_Tariffs" and "complaints") Which all have a common column "Tenant_ID" and Bill_#
Can someone help me out with the way forward to include this additional functionality to the existing query below?
I will highly appreciate for your contributionS. Thanks
SELECT BillSummary1.*
FROM BillSummary AS BillSummary1
WHERE NOT EXISTS (
SELECT 1 FROM BillSummary AS BillSummary2
WHERE (BillSummary2.[Tenant Name] = BillSummary1.[Tenant Name])
AND (BillSummary2.[Current Reading Date] > BillSummary1.[Current Reading Date]
OR (BillSummary2.[Tenant Name] = BillSummary1.[Tenant Name])
AND BillSummary2.ID > BillSummary1.ID)
)
You may add the inner join in the not exist just after BillSummary2 eg.
SELECT BillSummary1.*
FROM BillSummary AS BillSummary1
WHERE NOT EXISTS (
SELECT 1 FROM BillSummary AS BillSummary2
INNER JOIN
Taxes_Tariffs ON Taxes_Tariffs.Tenant_ID = BillSummary2.Tenant_ID AND
Taxes_Tariffs.Bill_NO = BillSummary2.Bill_NO
INNER JOIN
complaints ON complaints.Tenant_ID = BillSummary2.Tenant_ID AND
complaints.Bill_NO = BillSummary2.Bill_NO
WHERE (BillSummary2.[Tenant Name] = BillSummary1.[Tenant Name])
AND (BillSummary2.[Current Reading Date] > BillSummary1.[Current Reading Date]
OR (BillSummary2.[Tenant Name] = BillSummary1.[Tenant Name])
AND BillSummary2.ID > BillSummary1.ID)
)
if you would like to include these columns in your initial select you may use the following:
SELECT
BillSummary1.*,
Taxes_Tariffs.*,
complaints.*
FROM BillSummary AS BillSummary1
INNER JOIN
Taxes_Tariffs ON Taxes_Tariffs.Tenant_ID = BillSummary1.Tenant_ID AND
Taxes_Tariffs.Bill_NO = BillSummary1.Bill_NO
INNER JOIN
complaints ON complaints.Tenant_ID = BillSummary1.Tenant_ID AND
complaints.Bill_NO = BillSummary1.Bill_NO
WHERE NOT EXISTS (
SELECT 1 FROM BillSummary AS BillSummary2
WHERE (BillSummary2.[Tenant Name] = BillSummary1.[Tenant Name])
AND (BillSummary2.[Current Reading Date] > BillSummary1.[Current Reading Date]
OR (BillSummary2.[Tenant Name] = BillSummary1.[Tenant Name])
AND BillSummary2.ID > BillSummary1.ID)
)

Using an aggregate function as part of an expression

Simple question, I assume it is impossible to use an aggregate function as part of an expression but here is basically what I want to do:
SELECT ((Sum(RESOURCES.[Hours Spent]*EMPLOYEES.[Employee Rate]))
+ GRANTS.Overhead) AS [Estimated Development Cost]
FROM GRANTS
INNER JOIN
(EMPLOYEES
INNER JOIN RESOURCES ON EMPLOYEES.[Employee Number] = RESOURCES.[Employee Number])
ON GRANTS.[Grant Program Number] = RESOURCES.[Grant Program Number];
It works perfectly before I try to add that last value to it (Grants.Overhead). How could I go about doing this?
Thanks.
You have to put the Grants.Overhead in a separate query, as it counts only once per group. So here you go:
SELECT T1.EDC + GRANTS.Overhead AS [Estimated Development Cost]
FROM (SELECT RESOURCES.[Grant Program Number] AS GPN,
(Sum(RESOURCES.[Hours Spent] * EMPLOYEES.[Employee Rate])) AS EDC
FROM EMPLOYEES
INNER JOIN
RESOURCES
ON EMPLOYEES.[Employee Number] = RESOURCES.[Employee Number]
GROUP BY RESOURCES.[Grant Program Number]) AS T1
INNER JOIN
GRANTS
ON GRANTS.[Grant Program Number] = T1.GPN;
You need to sum() the overhead too, otherwise you will get the old "it is not contained in either an aggregate function or the GROUP BY clause" error.
This works:
SELECT ((Sum(RESOURCES.[Hours Spent]*EMPLOYEES.[Employee Rate])) + sum(GRANTS.Overhead)) AS [Estimated Development Cost]
FROM GRANTS
INNER JOIN (EMPLOYEES
INNER JOIN RESOURCES ON EMPLOYEES.[Employee Number] = RESOURCES.[Employee Number])
ON GRANTS.[Grant Program Number] = RESOURCES.[Grant Program Number];

SQL Query Second sum column squaring results

If I run 2 queries separately I get results like this..
select A.ACTNUMST, sum(B.EXTDCOST) as [IV Total]
from GL00105 A
INNER JOIN SEE30303 B on A.ACTINDX = B.IVIVINDX
group by A.ACTNUMST
Results -
Account No IV Total
2101-00-137 2033.60
4101-00-137 83765.86
6101-00-137 301984.23
Second Query
select A.ACTNUMST as [Account No], SUM(C.PERDBLNC) as [GL Total]
from GL00105 A
LEFT JOIN GL10110 C on A.ACTINDX = C.ACTINDX
group by A.ACTNUMST
Results -
Account No GL Total
2101-00-137 2033.60
4101-00-137 83765.86
6101-00-137 302656.23
I want to be able to join both results together to compare but I believe it is repeating the sum for each line in the GL total and then summing it again, it comes out with large numbers like -
select A.ACTNUMST as [Account No], sum(B.EXTDCOST) as [IV Total], SUM(C.PERDBLNC) as [GL Total]
from GL00105 A
INNER JOIN SEE30303 B on A.ACTINDX = B.IVIVINDX
LEFT JOIN GL10110 C on A.ACTINDX = C.ACTINDX
group by A.ACTNUMST
Results -
Account No IV Total GL Total
2101-00-137 2033.60 14235.20
4101-00-137 83765.86 116350696.20
6101-00-137 301984.23 1612897825.84
When it should be
Results -
Account No IV Total GL Total
2101-00-137 2033.60 2033.60
4101-00-137 83765.86 83765.86
6101-00-137 301984.23 302656.23
Please advise how to use the sum function to get the correct results.
Do you want something like this? Please check and comment if this is not what is expected.
Also please rectify any syntax errors.
SELECT T1.[Account No], T1.[IV Total], T2.[GL Total] FROM
(
select A.ACTNUMST as [Account No], sum(B.EXTDCOST) as [IV Total]
from GL00105 A
INNER JOIN SEE30303 B on A.ACTINDX = B.IVIVINDX
group by A.ACTNUMST
) T1
INNER JOIN
(
select A.ACTNUMST as [Account No], SUM(C.PERDBLNC) as [GL Total]
from GL00105 A
LEFT JOIN GL10110 C on A.ACTINDX = C.ACTINDX
group by A.ACTNUMST
) T2
ON T1.[Account No] = T2.[Account No]
Hope this helps
this can be done with the multiple cte also
with IVTotal as
(
select A.ACTNUMST, sum(B.EXTDCOST) as [IV Total]
from GL00105 A
INNER JOIN SEE30303 B
on A.ACTINDX = B.IVIVINDX
group by A.ACTNUMST
),
GLTotal as
(
select A.ACTNUMST as [Account No], SUM(C.PERDBLNC) as [GL Total]
from GL00105 A
LEFT JOIN GL10110 C
on A.ACTINDX = C.ACTINDX
group by A.ACTNUMST
)
select a.*,b.[GL Total]
from IVTotal a ,GLTotal b
where a.ACTNUMST = b.aCTNUMST

How to search for variable date in Access sql

I'm a bit confused why my ACCESS SQL query would act differently... When I do a SELECT query with the NOW() parameter, I get the right result. But if I use the TempVar!Date with todays date it returns 0...The queries look as follows:
This one works:
SELECT [Account Transactions]., Categories.,
IIf([Categories].[Income/Expense]="Expense",-([Account
Transactions]![Transaction Amount]),[Account
Transactions]![Transaction Amount]) AS [Actual Amount] FROM [Account
Transactions] LEFT JOIN Categories ON [Account Transactions].Category
= Categories.ID WHERE month([Account Transactions].[Entry Date]) = month(Now());
This one doesn't work:
SELECT [Account Transactions]., Categories., IIf([Categories].
[Income/Expense]="Expense",-([Account Transactions]![Transaction
Amount]),[Account Transactions]![Transaction Amount]) AS [Actual
Amount] FROM [Account Transactions] LEFT JOIN Categories ON [Account
Transactions].Category = Categories.ID WHERE month([Account
Transactions].[Entry Date]) = month([TempVars]![Date]);
I need the second one - with my variable to work.bYour help will be greatly appreciated. I've tested the tempvar value, and it's fine.
Try this:
SELECT [Account Transactions].*, Categories.*, IIf([Categories].[Income/Expense]="Expense",-([Account Transactions]![Transaction Amount]),[Account Transactions]![Transaction Amount]) AS [Actual Amount]
FROM [Account Transactions]
LEFT JOIN Categories ON [Account Transactions].Category = Categories.ID
WHERE month([Account Transactions].[Entry Date]) = month([TempVars]![Date]);
I've removed some extra spaces and added in asterisks where needed.
Your TempVars values will only last for your current open session of MS Access. What do you get when you enter ?Application.TempVars("Date").Value in your VBA window?

SQL joins and group by with 3 separate tables

I have two separate queries here that I need to make into one query, I'll post the queries, then try to explain what I'm trying to do.
SELECT Distinct I.ITMCDE, V.VNDRCDE, V.VNAME
FROM (SELECT RIGHT(Items.[Item Number], 3) as ITMCDE FROM Items) I,
(SELECT LEFT(Vendors.[Vendor ID], 3) as VNDRCDE,
Vendors.[Vendor Name] as VNAME
FROM Vendors) V
WHERE I.ITMCDE = V.VNDRCDE
In this first one, I simply match up the vendor code with the item code, to get the vendor name that produces the item.
SELECT DISTINCT (Items.[Item Description]), ItemQuantities.[QTY Available],
Items.[Selling U Of M], Items.[Item Number]
FROM ItemQuantities
INNER JOIN Items ON ItemQuantities.[Item Number] = Items.[Item Number]
WHERE Items.[Item Number] LIKE 'WH%'
AND Items.[Item Number] NOT LIKE '%RMW'
In this second one I'm selecting the item description, quantity available from two separate tables (quantity available is in a different table, match them up using the item number)
As you can see, the only correlation between the three tables is the item number, and not even that in the vendors table. The last three characters of the item number correlate with the first three characters of the vendor id... I did not design this setup. I'm just trying to work with it now.
How do I join these two statements into one single statement that will give me the vendor name, item description, Unit of Measure (Selling U of M), and item quantity where the item description is unique?
I think this should work:
SELECT DISTINCT Items.[Item Description],
ItemQuantities.[QTY Available],
Items.[Selling U Of M],
Items.[Item Number],
V.VNAME
FROM ItemQuantities
INNER JOIN Items ON ItemQuantities.[Item Number] = Items.[Item Number]
INNER JOIN Vendors ON
RIGHT(Items.[Item Number], 3) = LEFT(Vendors.[Vendor ID], 3)
WHERE Items.[Item Number] LIKE 'WH%'
AND Items.[Item Number] NOT LIKE '%RMW'
You can join it in:
SELECT DISTINCT (i.[Item Description]), iq.[QTY Available],
i.[Selling U Of M], i.[Item Number],
V.VNDRCDE, V.VNAME
FROM ItemQuantities iq INNER JOIN
Items i
ON iq.[Item Number] = i.[Item Number] left outer join
Vendors v
on LEFT(v.[Vendor ID], 3) = RIGHT(i.[Item Number], 3)
WHERE i.[Item Number] LIKE 'WH%' and
i.[Item Number] NOT LIKE '%RMW'
I am not sure if you intend anything special with the parentheses around i.[Item Description]. The distinct keyword applies to the entire row.
If you want distinct only on the description, then you need to use group by. Something like:
SELECT i.[Item Description],
max(iq.[QTY Available]),
max(i.[Selling U Of M]), max(i.[Item Number]),
max(V.VNDRCDE), max(V.VNAME)
FROM ItemQuantities iq INNER JOIN
Items i
ON iq.[Item Number] = i.[Item Number] left outer join
Vendors v
on LEFT(v.[Vendor ID], 3) = RIGHT(i.[Item Number], 3)
WHERE i.[Item Number] LIKE 'WH%' and
i.[Item Number] NOT LIKE '%RMW'
group by i.[Item Description])
The max() will return the maximum value. If all are the same, then this is a good way to get an "arbitrary" value.
#sgeddes Thank you.
This is what I came up with
SELECT DISTINCT
Item.ITEMNMBR AS [Item Number],
Item.ITEMDESC AS [Item Description],
Item.ITMGEDSC AS [Item Category],
Item.SELNGUOM AS [Unit of Measure],
(SELECT VENDNAME FROM PM00200 WHERE (VENDORID = IV00103.VENDORID)) AS [Vendor Name],
(CASE WHEN Quan.QTYONHND > 0 THEN 'In Stock' ELSE 'Out of Stock' END) AS [Stock Status]
FROM IV00101 AS Item INNER JOIN
IV00102 AS Quan ON Item.ITEMNMBR = Quan.ITEMNMBR INNER JOIN
IV00103 ON Item.ITEMNMBR = IV00103.ITEMNMBR AND Quan.ITEMNMBR = IV00103.ITEMNMBR
WHERE (Item.ITEMNMBR LIKE 'WH%') AND (IV00103.VENDORID NOT LIKE '%MIL')