Solved

Problems with "IN" clause

Posted on 2003-12-11
25
349 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:Scott Pletcher
ID: 9922492
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
ID: 9922557
That returned an identical recordset as the one I'm questioning. 414335 wasn't returned.
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 9922586
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

by:Kayser22
ID: 9922702
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:Scott Pletcher
ID: 9922780
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
ID: 9922799
Does the datatype ofjobno match in both the tables?
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 9922804
That's an EXCELLENT question, too.
0
 
LVL 3

Expert Comment

by:pra_kumar03
ID: 9922854
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
ID: 9922916
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:Scott Pletcher
ID: 9922943
?

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
ID: 9922966
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
 

Author Comment

by:Kayser22
ID: 9923135
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
ID: 9924373
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
ID: 9927215
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
ID: 9927254
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
ID: 9928247
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
ID: 9941260
Just trying an elimination process here.....

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

Author Comment

by:Kayser22
ID: 9942453
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
ID: 9942525
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
ID: 10005009
PAQed, with points refunded (125)

Computer101
E-E Admin
0
 
LVL 18

Expert Comment

by:ShogunWade
ID: 10009843
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
ID: 10012466
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
ID: 10015239
Ok.  Sounds fair enough.   I just was a bit mistified following a spell of silence and then a closure request.
0

Featured Post

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
I have a large data set and a SSIS package. How can I load this file in multi threading?
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

778 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