Solved

SQL - NOT IN

Posted on 2013-11-27
6
309 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 

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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Complex SQL script 1 31
Sharepoint 2010 Product Configuration Wizard fails 4 42
SQL Server Reporting Services Service Start Timeout 4 47
SQL Query 2 43
I have written a PowerShell script to "walk" the security structure of each SQL instance to find:         Each Login (Windows or SQL)             * Its Server Roles             * Every database to which the login is mapped             * The associated "Database User" for this …
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…
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…

932 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

7 Experts available now in Live!

Get 1:1 Help Now