September 10, 2008 at 2:32 pm
I have these queries:
1a - select * from import_cc_requests where reference not in (select reference from import_cc_payments )
or
1b - select * from import_cc_requests where rtrim(reference) not in (select rtrim(reference) from import_cc_payments )
- both these are returning 0 rows
2 - select * from import_cc_requests where reference='012408-01'
- this returns 1 row
3 - select * from import_cc_payments where reference='012408-01'
- this returns 0 rows
Reference field is varchar(255) in both tables, same collation (database default).
shouldn't 1a / 1b queries return at least this 1 row????
September 10, 2008 at 3:31 pm
First, I don't see any difference between the two queries. I am assuming you meant to modify the second query to something else.
As for your problem - can your sub-query return any null values? If so, then that will cause the problem you are seeing. To correct it, modify the sub-query to exclude nulls.
Jeffrey Williams
“We are all faced with a series of great opportunities brilliantly disguised as impossible situations.”
― Charles R. Swindoll
How to post questions to get better answers faster
Managing Transaction Logs
September 10, 2008 at 6:30 pm
two things i see...can reference be null in the payments? also i prefer to use a left join syntax instead..
1a - select * from import_cc_requests where reference not in (select reference from import_cc_payments WHERE REFERENCE IS NOT NULL)
or
select import_cc_requests.*
from import_cc_requests
LEFT OUTER JOIN import_cc_payments
ON import_cc_requests .REFERENCE = import_cc_payments.REFERENCE
WHERE import_cc_payments.REFERENCE IS NULL
Lowell
September 10, 2008 at 8:22 pm
Lowell (9/10/2008)
two also i prefer to use a left join syntax instead..
Check the execution plans of both on a set of big tables sometime...
--Jeff Moden
Change is inevitable... Change for the better is not.
September 10, 2008 at 9:17 pm
Sorry guys, I updated the 1b query, this is supposed to be "rtrim(reference)" instead of just "reference".
And reference doesn't have any NULL value in the 2 above mentioned tables ...
It should be something too simple - data comes from two Excel worksheets ....
I just want to list the rows in the requests table that don't have any payment associated, and there are around ~ 200 of them and both the first two queries returns 0 rows .... The value is just a random value picked from the ..._requests table ...
This is really mind-boggling - something like this never happened to me before...
September 10, 2008 at 10:38 pm
virgilrucsandescu (9/10/2008)
I have these queries:1a - select * from import_cc_requests where reference not in (select reference from import_cc_payments )
or
1b - select * from import_cc_requests where rtrim(reference) not in (select rtrim(reference) from import_cc_payments )
- both these are returning 0 rows
2 - select * from import_cc_requests where reference='012408-01'
- this returns 1 row
3 - select * from import_cc_payments where reference='012408-01'
- this returns 0 rows
Reference field is varchar(255) in both tables, same collation (database default).
shouldn't 1a / 1b queries return at least this 1 row????
Should work the way you've shown it. Have you paraphrased a real query or are these the actual real queries you've tried?
--Jeff Moden
Change is inevitable... Change for the better is not.
September 11, 2008 at 2:41 am
what is the rowcount of import_cc_payments table ?
karthik
September 11, 2008 at 6:47 am
1 - select count(*) from import_cc_requests = 307 rows
2 - select count(*) from import_cc_requests where reference in (select reference from import_cc_payments ) = 113 rows
3 - select count(*) from import_cc_requests where reference not in (select reference from import_cc_payments ) = 0 rows ??????
4 - select count(*) from import_cc_requests where reference is null - 0 rows
5 - select count(*) from import_cc_requests where rtrim(reference)='' - 0 rows
Shouldn't 1 = 2 + 3 ?????
September 11, 2008 at 7:03 am
Does using table aliases make any difference? (I know it shouldn't...)
select count(*) from import_cc_requests r where r.reference not in (select reference r2 from import_cc_payments r2 )
September 11, 2008 at 8:21 am
i can emulate the same error if the payments table has a row with nulls in the REFERENCE column.
other than that, maybe it's a difference in coallation between the two tables?or maybe comparing nvarchar to a varchar or something?
lets see the definition of the REFERENCE column for both tables...
CREATE TABLE tmp1(REFERENCE NCHAR(20),OTHERSTUFF VARCHAR(10) )
CREATE TABLE tmp2(REFERENCE NCHAR(30),OTHERSTUFF VARCHAR(10) )
INSERT INTO tmp1 values (N'REF1','example')
INSERT INTO tmp1 values (N'REF2','example')
INSERT INTO tmp1 values (N'REF3','example')
INSERT INTO tmp2 values (N'REF1','example')
INSERT INTO tmp2 values (NULL,'example')
select count(*) from tmp1 where reference not in (select reference from tmp2 )
drop table tmp1
drop table tmp2
Lowell
September 11, 2008 at 4:57 pm
Try different set of queries:
1 - select count(*) from import_cc_payments where reference IS NULL
2 - select count(*) from import_cc_requests R where EXISTS (select 1 from import_cc_payments P WHERE P.reference = R.reference)
3 - select count(*) from import_cc_requests R where NOT EXISTS (select 1 from import_cc_payments P WHERE P.reference = R.reference)
_____________
Code for TallyGenerator
Viewing 11 posts - 1 through 11 (of 11 total)
You must be logged in to reply to this topic. Login to reply