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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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

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

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
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

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.
chokkaStudentAuthor Commented:
@ste5an .. This has more than a million record
We need only Join query

Performance is a big issue
chokkaStudentAuthor Commented:
ste5an, syntax complies .. I couldn't get the expected output
chokkaStudentAuthor Commented:
Thanks
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.