Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Possible to get distinct record only in a union query

Posted on 2013-01-24
15
Medium Priority
?
443 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 66

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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 2000 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 46

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 31

Expert Comment

by:hnasr
ID: 38817170
try:

SELECT DISTINCT * FROM (your union queries)
0

Featured Post

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

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

Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…

636 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