SQL Left Outer Join to find missing rows

I have two table BLINES and BPATIENT.  
BPATIENT should have identical number of lines as BLINES.

In my scenario, i am missing some lines for BPATIENT.  Line_Num is the column which has line numbers such as 1 2 3 4 5 ..

I need a Left Join query to find missing line_num in BPATIENT Table.

I wrote my query .. which is not working. I am not able to figure it out ..

SELECT				A.LINE_NUM,A.DOC_ID 
FROM				BLINES A 
LEFT OUTER JOIN		BPATIENT B
ON					A.DOC_ID = B.DOC_ID
AND				A.LINE_NUM = B.LINE_NUM
WHERE				A.LINE_NUM is null

Open in new window

chokkaStudentAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ste5anSenior DeveloperCommented:
You need to test the outer tables column for NULL, thus B.LINE_NUM, not A:

SELECT  A.LINE_NUM ,
        A.DOC_ID
FROM    BLINES A
        LEFT OUTER JOIN BPATIENT B ON A.DOC_ID = B.DOC_ID
                                      AND A.LINE_NUM = B.LINE_NUM
WHERE   B.LINE_NUM IS NULL;

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Vitor MontalvãoMSSQL Senior EngineerCommented:
You can also do it without a LEFT JOIN:
SELECT A.LINE_NUM,A.DOC_ID 
FROM BLINES A 
WHERE NOT EXISTS (SELECT 1
	FROM BPATIENT B
	WHERE A.DOC_ID = B.DOC_ID AND	A.LINE_NUM = B.LINE_NUM)

Open in new window

0
Deepak ChauhanSQL Server DBACommented:
You can do like this as well

SELECT  LINE_NUM , DOC_ID
FROM    BLINES
except
SELECT  LINE_NUM , DOC_ID
FROM    BPATIENT
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

ste5anSenior DeveloperCommented:
The LEFT JOIN and the EXISTS approach use often the same query plan. The except approach, while being much more simple to read can be slower in execution. Thus compare the execution plans, the necessary IO and the execution time.
1
chokkaStudentAuthor Commented:
@ste5an .. This has more than a million record
We need only Join query

Performance is a big issue
0
chokkaStudentAuthor Commented:
ste5an, syntax complies .. I couldn't get the expected output
0
chokkaStudentAuthor Commented:
Thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.