Solved

Possible to get distinct record only in a union query

Posted on 2013-01-24
15
432 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

691 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