Solved

SQL - NOT IN

Posted on 2013-11-27
6
310 Views
Last Modified: 2013-11-27
Hello all,
I have the following tables:
Rxs has columns rxno,patid,rono,drug
Rxadmin has columns rxno,patid,rono,admintime.
How to write a query to find all the rono which are in Rxs table but not present in Rxadmin table.
I wrote the below sql
SELECT rono,patid
       
FROM   [rx].[dbo].[rxs] 
WHERE  rxs.rono NOT IN (SELECT rono
                             FROM   [rx].[dbo].[rxadmintimes]
                             WHERE  rono IS NOT NULL) 
                             order by rxs.RoNo

Open in new window

Somehow its not giving the right results
0
Comment
Question by:Star79
6 Comments
 
LVL 9

Expert Comment

by:guswebb
ID: 39680654
select * from rxs where not rxno in (select rxno from rxadmin)
0
 
LVL 26

Expert Comment

by:Shaun Kline
ID: 39680677
Your query is syntactically correct. Are you missing rows in your results or do you have extra rows?
0
 
LVL 9

Expert Comment

by:guswebb
ID: 39680722
sorry, that should have been...

select * from rxs where not rono in (select rono from rxadmin)
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:Star79
ID: 39680919
my query is missing rows
I also tried
select * from rxs where not rono in (select rono from rxadmin)
still missing rows
0
 
LVL 65

Accepted Solution

by:
Jim Horn earned 500 total points
ID: 39680933
>find all the rono which are in Rxs table but not present in Rxadmin table.
SELECT r.rono, r.patid
FROM rxs r
-- The LEFT JOIN ensures that all rxs rows are in the return set
LEFT JOIN Rxadmin ra ON r.rono = ra.rono  
-- And then limits the return set to only those with no Rxadmin match
   WHERE ra.rono IS NULL

Open in new window


>where not rxno in
Not sure if it matters, but in the above T-SQL try where Rxs.rxno NOT IN
0
 
LVL 26

Expert Comment

by:Shaun Kline
ID: 39680934
Try this:
SELECT rono,patid
FROM   [rx].[dbo].[rxs] rxs
WHERE  NOT EXISTS (SELECT 1
                             FROM   [rx].[dbo].[rxadmintimes]
                             WHERE  rxs.rono = rono
                             AND rxs.rxno = rxno) 
ORDER BY rxs.RoNo

Open in new window


This SQL assumes that RXNo is a linking field between the two tables along with rono.

The NOT EXISTS is considered more efficient that using a NOT IN query.
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

777 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