Solved

Possible to get distinct record only in a union query

Posted on 2013-01-24
15
426 Views
Last Modified: 2013-02-02
Here is my union query coming from two select queries...

select * from qryTestNumber1
where partN = Forms!frmShipments!cboPartN
UNION select * from qryTestNumber2
where partN = Forms!frmShipments!cboPartN;

But I am getting two records in the result of the union query if both select queries have the same result.  I only want to see one of the records in the result.

Is there a way to do this?
0
Comment
Question by:SteveL13
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
  • 2
  • +3
15 Comments
 
LVL 26

Expert Comment

by:jerryb30
ID: 38816033
Are all fields in both queries the same values in terms of content? Order of fields?

If so, it should naturally give only distinct results.

Can you post the SQL of the 2 queries?
0
 

Author Comment

by:SteveL13
ID: 38816071
Here's the first qry:

SELECT tblFinGoods.PartN, tblOpenOrders.Ack_Date, tblOpenOrders.Purchase_Order, tblOpenOrders.Order_Qty, tblOpenOrders.RecID, tblFinGoods.Serial, tblPartsMasters.Serial, tblOpenOrders.AppliedInvQty, tblOpenOrders.Customer_1, tblOpenOrders.Complete
FROM (tblFinGoods LEFT JOIN tblOpenOrders ON tblFinGoods.OpenOrdRecID = tblOpenOrders.RecID) LEFT JOIN tblPartsMasters ON tblOpenOrders.Part_No = tblPartsMasters.Part_No
WHERE (((tblFinGoods.PartN)=[Forms]![frmShipments]![cboPartN3]) AND ((tblOpenOrders.Complete)=False))
ORDER BY tblFinGoods.PartN, tblOpenOrders.Ack_Date;

And here's the 2nd query:

SELECT tblFinished.PartN, tblOpenOrders.Ack_Date, tblOpenOrders.Purchase_Order, tblOpenOrders.Order_Qty, tblOpenOrders.RecID, tblFinished.Serial, tblPartsMasters.Serial, tblOpenOrders.AppliedInvQty, tblOpenOrders.Customer_1, tblOpenOrders.Complete
FROM (tblFinished LEFT JOIN tblOpenOrders ON tblFinished.OpenOrdRecID = tblOpenOrders.RecID) INNER JOIN tblPartsMasters ON tblFinished.PartN = tblPartsMasters.Part_No
WHERE (((tblFinished.PartN)=[Forms]![frmShipments]![cboPartN3]) AND ((tblOpenOrders.Complete)=False))
ORDER BY tblFinished.PartN, tblOpenOrders.Ack_Date;

And here's the union query (revised sine I put the form criteria in the first two queries:

select * from qryTestNumber1
UNION select * from qryTestNumber2;
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 38816083
UNION returns only distinct rows, which would EXclude duplicates.

UNION ALL returns all rows, which would INclude duplicates.

I agree with jerryb30, there has to be a column somewhere that contains a different value that is causing the query to return both rows.
0
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
LVL 26

Expert Comment

by:jerryb30
ID: 38816084
And each query returns the same values for each field in a single record, when run separately?
0
 

Author Comment

by:SteveL13
ID: 38816104
Yes...  each query returns the same values for each field in a single record, when run separately  and in the exact same order.
0
 

Author Comment

by:SteveL13
ID: 38816119
Attached:  Screen shots of the two queries.
Clip1.jpg
Clip2.jpg
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 38816129
try running this query


SELECT T.PartN, T.Ack_Date, T.Purchase_Order, T.Order_Qty, T.RecID, T.Serial, tblPartsMasters.Serial, T.AppliedInvQty, T.Customer_1, T.Complete
From
(
SELECT tblFinGoods.PartN, tblOpenOrders.Ack_Date, tblOpenOrders.Purchase_Order, tblOpenOrders.Order_Qty, tblOpenOrders.RecID, tblFinGoods.Serial, tblPartsMasters.Serial, tblOpenOrders.AppliedInvQty, tblOpenOrders.Customer_1, tblOpenOrders.Complete
FROM (tblFinGoods LEFT JOIN tblOpenOrders ON tblFinGoods.OpenOrdRecID = tblOpenOrders.RecID) LEFT JOIN tblPartsMasters ON tblOpenOrders.Part_No = tblPartsMasters.Part_No
WHERE (((tblFinGoods.PartN)=Eval("[Forms]![frmShipments]![cboPartN3]") AND ((tblOpenOrders.Complete)=False))
ORDER BY tblFinGoods.PartN, tblOpenOrders.Ack_Date
UNION
SELECT tblFinished.PartN, tblOpenOrders.Ack_Date, tblOpenOrders.Purchase_Order, tblOpenOrders.Order_Qty, tblOpenOrders.RecID, tblFinished.Serial, tblPartsMasters.Serial, tblOpenOrders.AppliedInvQty, tblOpenOrders.Customer_1, tblOpenOrders.Complete
FROM (tblFinished LEFT JOIN tblOpenOrders ON tblFinished.OpenOrdRecID = tblOpenOrders.RecID) INNER JOIN tblPartsMasters ON tblFinished.PartN = tblPartsMasters.Part_No
WHERE (((tblFinished.PartN)=Eval("[Forms]![frmShipments]![cboPartN3]") AND ((tblOpenOrders.Complete)=False))
ORDER BY tblFinished.PartN, tblOpenOrders.Ack_Date
) AS T
Group by T.PartN, T.Ack_Date, T.Purchase_Order, T.Order_Qty, T.RecID, T.Serial, tblPartsMasters.Serial, T.AppliedInvQty, T.Customer_1, T.Complete



see if you will duplicates
0
 
LVL 26

Expert Comment

by:jerryb30
ID: 38816135
try
select distinct * from qryTestNumber1
union
select distinct * from qryTestNumber2

Given what you stated, this should not be happening, but it IS possible that a field is not being evaluated exactly the same.

Are the data types of fields in tblFinished and tblFinGoods the same?  Maybe there is some white space, based on field lengths, or a stray bit in a numeric field (maybe one being a single and another being a double.)
0
 

Author Comment

by:SteveL13
ID: 38816173
Capricorn1:  Syntax error in from clause
0
 

Author Comment

by:SteveL13
ID: 38816181
Jerryb30:  Still get duplicate records
0
 
LVL 26

Expert Comment

by:jerryb30
ID: 38816200
Not linked to external database? Exact same structure in the 2 tables?
0
 
LVL 26

Expert Comment

by:jerryb30
ID: 38816209
Oh, any memo fields?
0
 
LVL 120

Accepted Solution

by:
Rey Obrero (Capricorn1) earned 500 total points
ID: 38816229
ok, try this


SELECT T.PartN, T.Ack_Date, T.Purchase_Order, T.Order_Qty, T.RecID, T.Serial, T.Serial, T.AppliedInvQty, T.Customer_1, T.Complete
From
(
SELECT tblFinGoods.PartN, tblOpenOrders.Ack_Date, tblOpenOrders.Purchase_Order, tblOpenOrders.Order_Qty, tblOpenOrders.RecID, tblFinGoods.Serial, tblPartsMasters.Serial, tblOpenOrders.AppliedInvQty, tblOpenOrders.Customer_1, tblOpenOrders.Complete
FROM (tblFinGoods LEFT JOIN tblOpenOrders ON tblFinGoods.OpenOrdRecID = tblOpenOrders.RecID) LEFT JOIN tblPartsMasters ON tblOpenOrders.Part_No = tblPartsMasters.Part_No
WHERE (((tblFinGoods.PartN)=Eval("[Forms]![frmShipments]![cboPartN3]") AND ((tblOpenOrders.Complete)=False))
ORDER BY tblFinGoods.PartN, tblOpenOrders.Ack_Date
UNION
SELECT tblFinished.PartN, tblOpenOrders.Ack_Date, tblOpenOrders.Purchase_Order, tblOpenOrders.Order_Qty, tblOpenOrders.RecID, tblFinished.Serial, tblPartsMasters.Serial, tblOpenOrders.AppliedInvQty, tblOpenOrders.Customer_1, tblOpenOrders.Complete
FROM (tblFinished LEFT JOIN tblOpenOrders ON tblFinished.OpenOrdRecID = tblOpenOrders.RecID) INNER JOIN tblPartsMasters ON tblFinished.PartN = tblPartsMasters.Part_No
WHERE (((tblFinished.PartN)=Eval("[Forms]![frmShipments]![cboPartN3]") AND ((tblOpenOrders.Complete)=False))
ORDER BY tblFinished.PartN, tblOpenOrders.Ack_Date
) AS T
Group by T.PartN, T.Ack_Date, T.Purchase_Order, T.Order_Qty, T.RecID, T.Serial, tblPartsMasters.Serial, T.AppliedInvQty, T.Customer_1, T.Complete
0
 
LVL 45

Expert Comment

by:aikimark
ID: 38816881
please check the exact value of the date fields.  If there is a time-of-day (fractional value) with these dates, they might be different.
0
 
LVL 30

Expert Comment

by:hnasr
ID: 38817170
try:

SELECT DISTINCT * FROM (your union queries)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

696 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