![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
Hi, Why is this returning records that exist in the first table (tblRoomAssignmentTemp) that are not in the second table (tblMoneyReceived)? From a SQL book that I have and from what I have found on the Internet, this is the proper syntax and therefore shouldn't be happening. Maybe I messed something up: Select tblRoomAssignmentTemp.reservationid From tblRoomAssignmentTemp Where NOT EXISTS (SELECT tblMoneyReceived.ReservationID, tblMoneyReceived.PaymentCategory FROM tblMoneyReceived Where tblMoneyReceived.ReservationId = tblRoomAssignmentTemp.reservationid) I've tried Not IsNull(tblMoneyReceived.reservationid) but that makes no difference. However, I managed to build a query in the Query Builder that works (I can not entirely grasp Joins!): SELECT tblRoomAssignmentTemp.ReservationID, tblMoneyReceived.PaymentCategory FROM tblRoomAssignmentTemp INNER JOIN tblMoneyReceived ON tblRoomAssignmentTemp.ReservationID = tblMoneyReceived.ReservationID WHERE (((tblRoomAssignmentTemp.ReservationID)>0) AND ((tblMoneyReceived.PaymentCategory)="Deposit")) ORDER BY tblRoomAssignmentTemp.ReservationID; |
#3
| ||||
| ||||
|
|
I'm glad you discovered on your own that Access' "flavor" of SQL, unless you specify otherwise, differs from the standard in some respects, and that researching in "an SQL book" can lead to frustration. Researching in an Access book that covers SQL, or searching online for Access SQL, will be much more productive. |
|
Is there some particular reason that you feel it is necessary to write your own SQL from scratch, rather than use the very helpful Query Builder that Access provides? |
|
It's really easy to use, and, if you need to later manipulate the SQL with VBA code, you can switch to SQL View to see what it generates. Sometimes it generates a few extra parentheses pairs, and there are some things it won't handle (UNION queries, for example, and subqueries). There's a good deal of information available in the newsgroup archives and at the Microsoft site regarding subqueries in Access. |
|
Anyway, glad you found something that works. Best of luck with using Access. |
#4
| |||
| |||
|
|
Hi, Why is this returning records that exist in the first table (tblRoomAssignmentTemp) that are not in the second table (tblMoneyReceived)? From a SQL book that I have and from what I have found on the Internet, this is the proper syntax and therefore shouldn't be happening. Maybe I messed something up: Select tblRoomAssignmentTemp.reservationid From tblRoomAssignmentTemp Where NOT EXISTS (SELECT tblMoneyReceived.ReservationID, tblMoneyReceived.PaymentCategory FROM tblMoneyReceived Where tblMoneyReceived.ReservationId = tblRoomAssignmentTemp.reservationid) I've tried Not IsNull(tblMoneyReceived.reservationid) but that makes no difference. However, I managed to build a query in the Query Builder that works (I can not entirely grasp Joins!): SELECT tblRoomAssignmentTemp.ReservationID, tblMoneyReceived.PaymentCategory FROM tblRoomAssignmentTemp INNER JOIN tblMoneyReceived ON tblRoomAssignmentTemp.ReservationID = tblMoneyReceived.ReservationID WHERE (((tblRoomAssignmentTemp.ReservationID)>0) AND ((tblMoneyReceived.PaymentCategory)="Deposit")) ORDER BY tblRoomAssignmentTemp.ReservationID; |
#5
| |||
| |||
|
|
I'z wrong! The query is NOT listing just records from the first table (tlbRoomAssignmentTemp) that don't exist in the second (tblMoneyReceived) |
|
that haven't made a deposit payment. |
#6
| |||
| |||
|
|
However, I managed to build a query in the Query Builder that works (I can not entirely grasp Joins!): SELECT tblRoomAssignmentTemp.ReservationID, tblMoneyReceived.PaymentCategory FROM tblRoomAssignmentTemp INNER JOIN tblMoneyReceived ON tblRoomAssignmentTemp.ReservationID = tblMoneyReceived.ReservationID WHERE (((tblRoomAssignmentTemp.ReservationID)>0) AND ((tblMoneyReceived.PaymentCategory)="Deposit")) ORDER BY tblRoomAssignmentTemp.ReservationID; |
#7
| |||
| |||
|
|
I used to believe the EXISTS version was more efficient than the IN version, but I've had some doubts raised about this in the past couple years, so test both and use whichever performs better. The problem with using the exclusion join is that if there can be multiple records with the same ReservationId in tblMoneyReceived, then you will get multiple records returned even if only a single record exists in tblRoomAssignmentTemp. The subquery solutions avoid that trap. |
#8
| |||||
| |||||
|
|
I'z wrong! The query is NOT listing just records from the first table (tlbRoomAssignmentTemp) that don't exist in the second (tblMoneyReceived) .I was wondering about that. The query you said was working used an INNER JOIN which is the wrong type of join to exclude records. You need to change it to a LEFT OUTER JOIN and add a criterion to exclude records where a key field from the right side of the join is null: SELECT tblRoomAssignmentTemp.ReservationID, tblMoneyReceived.PaymentCategory FROM tblRoomAssignmentTemp LEFT JOIN tblMoneyReceived ON tblRoomAssignmentTemp.ReservationID = tblMoneyReceived.ReservationID WHERE (((tblRoomAssignmentTemp.ReservationID)>0) AND ((tblMoneyReceived.ReservationID) is null)) ORDER BY tblRoomAssignmentTemp.ReservationID; Having said that, I am very puzzled that the WHERE NOT EXISTS construct did not work. It should have looked like this: Select tblRoomAssignmentTemp.reservationid From tblRoomAssignmentTemp Where NOT EXISTS (SELECT * FROM tblMoneyReceived Where tblMoneyReceived.ReservationId = tblRoomAssignmentTemp.reservationid) Are you telling me that this query returns records where tblMoneyReceived.ReservationId = tblRoomAssignmentTemp.reservationid? |
|
Rats! I only want assigned rooms/reservations that haven't made a deposit payment. Oh wait - could that be the problem? You initially failed to specify that deposit payment criterion. It would look like this: |
|
Select tblRoomAssignmentTemp.reservationid From tblRoomAssignmentTemp Where NOT EXISTS (SELECT * FROM tblMoneyReceived Where tblMoneyReceived.ReservationId = tblRoomAssignmentTemp.reservationid and tblMoneyReceived.PaymentCategory="Deposit") Alternatively it would look like this: Select tblRoomAssignmentTemp.reservationid From tblRoomAssignmentTemp Where reservationid NOT IN (SELECT ReservationId FROM tblMoneyReceived Where and tblMoneyReceived.PaymentCategory="Deposit") |
|
I used to believe the EXISTS version was more efficient than the IN version, |
|
but I've had some doubts raised about this in the past couple years, so test both and use whichever performs better. The problem with using the exclusion join is that if there can be multiple records with the same ReservationId in tblMoneyReceived, then you will get multiple records returned even if only a single record exists in tblRoomAssignmentTemp . The subquery solutions avoid that trap. |
#9
| |||
| |||
|
|
PW <emailaddyinsig (AT) ifIremember (DOT) com> wrote in news:5kohv65ou6umq6bkiq014njqu1jcpt7d48 (AT) 4ax (DOT) com: However, I managed to build a query in the Query Builder that works (I can not entirely grasp Joins!): SELECT tblRoomAssignmentTemp.ReservationID, tblMoneyReceived.PaymentCategory FROM tblRoomAssignmentTemp INNER JOIN tblMoneyReceived ON tblRoomAssignmentTemp.ReservationID = tblMoneyReceived.ReservationID WHERE (((tblRoomAssignmentTemp.ReservationID)>0) AND ((tblMoneyReceived.PaymentCategory)="Deposit")) ORDER BY tblRoomAssignmentTemp.ReservationID; Using a JOIN is going to be more reliably efficient than NOT EXISTS with a subquery because NOT EXISTS (and NOT IN) are not reliably optimized by the Jet/ACE query optimizer to use the indexes on both sides of the comparison. A JOIN will always use the indexes on both sides, so if you can write the query using a JOIN, that's going to be preferable. |
#10
| |||
| |||
|
|
SELECT tblRoomAssignmentTemp.ReservationID, tblMoneyReceived.PaymentCategory FROM tblRoomAssignmentTemp INNER JOIN tblMoneyReceived ON tblRoomAssignmentTemp.ReservationID = tblMoneyReceived.ReservationID WHERE (((tblRoomAssignmentTemp.ReservationID)>0) AND ((tblMoneyReceived.PaymentCategory)="Deposit")) ORDER BY tblRoomAssignmentTemp.ReservationID; |
![]() |
| Thread Tools | |
| Display Modes | |
| |