Inconsistent results in a query with a UNION clause - missing records in result set

I created a query in SQL Server (2005) that includes a UNION clause.  If I run each of the queries separately (without a UNION) the provide the expected results.  When I run the query with the UNION clause the query executes (without errors or warnings)  I get results EXCEPT that I 'lose' records from the generated results set.

In writing the query I paid  particular attention to field data types to make sure they matched.  In the second query there is no Inner Join on the two tables - this was done be design so as not to lose certain records.

Any thoughts/consideration when doing this type of query?

Here is the syntax:

SELECT MatterID, MatterInfoMatterNum, MatterInfoSortName, ClientInfoClientID, TBankAllocInfoStatus, TBankAllocInfoCheckID, TBankAllocInfInvDate, TBankAllocInfoAmount, TBankAllocInfoEntryType, TBankAllocInfActivityID, TBankAllocInfExplanation, TBankCommInfStatus, TBankCommInfDate, TBankCommInfEntryType, TBankCommInfAmount, TBankCommInfPaidTo, ActivityCodesNickname, ActivityCodesName
 FROM   ((MattInf MattInf INNER JOIN TBAlloc TBAlloc ON MattInf.MatterID=TBAlloc.MatterID) INNER JOIN ActCode ActCode ON TBAlloc.TBankAllocInfActivityID=ActCode.ActivityCodesID) INNER JOIN TBComm TBComm ON TBAlloc.TBankAllocInfoCheckID=TBComm.TBankCommInfSequenceID
UNION
SELECT MatterID, MatterInfoMatterNum, MatterInfoSortName, ClientInfoClientID,
0 as TBankAllocInfoStatus, null as TBankAllocInfoCheckID, 0 asTBankAllocInfInvDate,
0 as TBankAllocInfoAmount, 0 as TBankAllocInfoEntryType, 0 as TBankAllocInfActivityID,
‘ ‘ as TBankAllocInfExplanation, 0 as TBankCommInfStatus, 0 as TBankCommInfDate,
0 as TBankCommInfEntryType, 0 as TBankCommInfAmount, ‘ ‘ as TBankCommInfPaidTo,
ActCode.ActivityCodesNickname, ActCode.ActivityCodesName
 FROM   MattInf, ActCode

Thanks for your input.

John
JRStpeteAsked:
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.

Rufus WilliamSenior Software ArchitectCommented:
if you are checking the record count and concluding that records are lost, probably this could be just a Illusion. Union actuallly performs a Distinct operation after executing both the queries, so that any records that are matched in the first query is skipped from the second query. In case you want all rows including the duplicate records try using UNION ALL

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
Raja Jegan RSQL Server DBA & Architect, EE Solution GuideCommented:
You are missing a JOIN condition in your Second SELECT query of UNION statement.
And use UNION ALL if duplicate records are filtered because of UNION.
0
JRStpeteAuthor Commented:
Don't know how I missed that! Perfect
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
BlackBerry

From novice to tech pro — start learning today.