Solved

SQL - NOT IN

Posted on 2013-11-27
6
308 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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

Suggested Solutions

After restoring a Microsoft SQL Server database (.bak) from backup or attaching .mdf file, you may run into "Error '15023' User or role already exists in the current database" when you use the "User Mapping" SQL Management Studio functionality to al…
Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

708 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

13 Experts available now in Live!

Get 1:1 Help Now