Solved

Problems with "IN" clause

Posted on 2003-12-11
25
347 Views
Last Modified: 2008-02-01
I wrote an sql sentence while working on a problem that doesn't work the way I expect it to. I was wondering if anyone can explain to me why this doesn't work.

select jobno from OEORD_T1 where jobno in (select jobno from UPSHH_T1 where OEUPDT_S="Y")

does not return jobno=414335

However

select jobno from OEORD_T1 where jobno in (123456,456789,414335)

returns 414335 and

select jobno from UPSHH_T1 where OEUPDT_S="Y"

returns 414335

Furthermore

select oeord_t1.jobno from OEORD_T1 inner join  UPSHH_t1
on oeord_t1.jobno=upshh_t1.jobno where OEUPDT_S='Y'

returns 414335. I used this last statement instead of the first one to get the data that I needed. I don't understand why the original one didn't return the data I expected it to. I'm wondering if I don't understand how to use the IN clause correctly. Can anyone see what I'm doing wrong? Thanks in advance.


0
Comment
Question by:Kayser22
  • 7
  • 6
  • 5
  • +3
25 Comments
 
LVL 69

Expert Comment

by:ScottPletcher
Comment Utility
NULLs can "short-circuit"/"goof up" IN processing, so try this:

select jobno
from OEORD_T1
where jobno in (
    select jobno
    from UPSHH_T1
    where jobno IS NOT NULL
    AND OEUPDT_S="Y" )

although the join method is generally preferred.
0
 

Author Comment

by:Kayser22
Comment Utility
That returned an identical recordset as the one I'm questioning. 414335 wasn't returned.
0
 
LVL 69

Expert Comment

by:ScottPletcher
Comment Utility
Other than using single quotes instead of double, I don't see anything wrong, and it's very unlikely that you have a column named  y  on UPSHH_T1 table.


select jobno
from OEORD_T1
where jobno in (
    select jobno
    from UPSHH_T1
    where jobno IS NOT NULL
    AND OEUPDT_S='Y' )
0
 

Author Comment

by:Kayser22
Comment Utility
Actually the single quote version is what I started with. That returns the same recordset as the other two.

I'm using WIN uSQLi32 by transoft to query a transoft database. I hate to blame all my problems on buggy software, but maybe... Anyway here is the query plan I get when I run the previous query.

(Query: 1) OEORD_T1 INDEXED
Index No. 0
Lower : Set OEORD_T1.JOBNO to the Lowest of Subquery 1.1
Upper : OEORD_T1.JOBNO <= the Highest of Subquery 1.1
With Filter : OEORD_T1.JOBNO =any Results of Subquery 1.1

(Query: 1.1) UPSHH_T1 SEQUENTIAL
FILTER : ((not UPSHH_T1.JOBNO = null) and UPSHH_T1.OEUPDT_S = 'Y')
0
 
LVL 69

Expert Comment

by:ScottPletcher
Comment Utility
Actually, buggy software is about the only explanation I can think of, either on the WIN uSQLi32 end or in SQL Server itself.  Otherwise, I would expect that query to work.
0
 
LVL 3

Expert Comment

by:pra_kumar03
Comment Utility
Does the datatype ofjobno match in both the tables?
0
 
LVL 69

Expert Comment

by:ScottPletcher
Comment Utility
That's an EXCELLENT question, too.
0
 
LVL 3

Expert Comment

by:pra_kumar03
Comment Utility
first of all try this query and see if it returns something or not
select jobno from OEORD_T1 where jobno in (select jobno from UPSHH_T1 where OEUPDT_S="Y" and jobno = 414335)
0
 

Author Comment

by:Kayser22
Comment Utility
select jobno from OEORD_T1 where jobno in (select jobno from UPSHH_T1 where OEUPDT_S="Y" and jobno = 414335)

does return 414335

The data types are the same, although in one table it is defined as a 3 byte field with a maximum of 999999 and the other table it is a 4 byte field with a maximum of 99999999.
0
 
LVL 69

Expert Comment

by:ScottPletcher
Comment Utility
?

What, specifically, are the two datatypes/column definitions?

Are they, for example, CHAR(3)/VARCHAR(3) and CHAR(4)/VARCHAR(4)?
0
 
LVL 3

Expert Comment

by:pra_kumar03
Comment Utility
In case it is of the type as told by Scott then u may try to do ltrim(rtrim(jobno) in all the places and run the query
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:Kayser22
Comment Utility
In transoft they are both defined as data type 2 which numeric.

Also, I didn't mean to imply anywhere that the problem sentence returns the null set. It does return 53 records; lowest= 406297 and highest =413256. I can't figure out why those get returned but 414335 does not.
0
 
LVL 15

Expert Comment

by:namasi_navaretnam
Comment Utility
Try this and see if the right rowcount is returned,

select count(*) from OEORD_T1 where jobno in (select jobno from UPSHH_T1 where OEUPDT_S='Y')

Or Try

select jobno from OEORD_T1 where jobno in (select jobno from UPSHH_T1 where OEUPDT_S="Y")

Select @@ROWCOUNT

Is it possible that row is filtered by your front end tool.
HTH

Namasi
0
 
LVL 18

Expert Comment

by:ShogunWade
Comment Utility
One thought.....    What is the collation on the OEUPDT_S field ?

If it is a case sensative collation you will need to UPPER()  your field before comparing.
0
 
LVL 18

Expert Comment

by:ShogunWade
Comment Utility
ignore last,  ive just read the question properly :S

What about trying this

select jobno from OEORD_T1 a where exists (select 1 from UPSHH_T1 where OEUPDT_S="Y" and jobno=a.jobno)

?
0
 

Author Comment

by:Kayser22
Comment Utility
Just catching up a bit. Due to normal operations, the original SQL sentence is now returning 11 records. 414335 is still in both tables.

select count(*) from OEORD_T1 where jobno in (select jobno from UPSHH_T1 where OEUPDT_S='Y')

returns 11

select jobno from OEORD_T1 where jobno in (select jobno from UPSHH_T1 where OEUPDT_S="Y")

returns the same recordset as the original sentence.

select jobno from OEORD_T1 a where exists (select 1 from UPSHH_T1 where OEUPDT_S="Y" and jobno=a.jobno)

Timed out before returning results

0
 
LVL 18

Expert Comment

by:ShogunWade
Comment Utility
Just trying an elimination process here.....

Are you sure there is no corruption in your database ?
0
 

Author Comment

by:Kayser22
Comment Utility
Am I sure? No. Ususally any corruption manifests itself in the indexes, and I've rebuilt those. Ive checked the records in questions directly and they don't look corrupted. I don't know how to check for any other sorts of corruption.
0
 
LVL 18

Expert Comment

by:ShogunWade
Comment Utility
try running DBCC CHECKDB('MyDb')

In the past I once had a table with a badly linked index page this meant that depending on whether the index was used depended upon whether the query returned the results :(

0
 
LVL 1

Accepted Solution

by:
Computer101 earned 0 total points
Comment Utility
PAQed, with points refunded (125)

Computer101
E-E Admin
0
 
LVL 18

Expert Comment

by:ShogunWade
Comment Utility
Computer101:

3 days wasnt a very long time to post comments about this request given that a) the author seems to have gone silent since my last post.   and b) 3 days included xmas day,boxing day and saturday  :( :(

I am confused as to why the request was made.    Did our commets not resolve the problem?   If so why hasnt the author said so?   Did the author find the problem?

0
 

Author Comment

by:Kayser22
Comment Utility
My apologies for not responding to your last entry. I started to and never finished. As I stated earlier, this isn't an SQL server. I believe the commands that you asked me to try are SQL server commands. I don't know of any equivalents in the transoft world. I originally asked this question to see if something was wrong with the SQL sentence I wrote, which I thought there probably was. Nobody found anything wrong with it. Unfortunately, I don't think that it is in the scope of my job, or my ability, to pursue this any further. Furthermore, the data changes on a daily basis, so my example is long gone. I don't want to waste any more of anybody's time, so I asked for the issue to be closed. I appreciate everybody's efforts at EE and don't want to treat anybody unfairly. If you can give me any feedback on how I could have handled this better, I'm all ears.

0
 
LVL 18

Expert Comment

by:ShogunWade
Comment Utility
Ok.  Sounds fair enough.   I just was a bit mistified following a spell of silence and then a closure request.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now