Solved

Possible to get distinct record only in a union query

Posted on 2013-01-24
15
388 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
  • 5
  • 5
  • 2
  • +3
15 Comments
 
LVL 26

Expert Comment

by:jerryb30
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 26

Expert Comment

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

Author Comment

by:SteveL13
Comment Utility
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
Comment Utility
Attached:  Screen shots of the two queries.
Clip1.jpg
Clip2.jpg
0
 
LVL 119

Expert Comment

by:Rey Obrero
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 26

Expert Comment

by:jerryb30
Comment Utility
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
Comment Utility
Capricorn1:  Syntax error in from clause
0
 

Author Comment

by:SteveL13
Comment Utility
Jerryb30:  Still get duplicate records
0
 
LVL 26

Expert Comment

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

Expert Comment

by:jerryb30
Comment Utility
Oh, any memo fields?
0
 
LVL 119

Accepted Solution

by:
Rey Obrero earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
try:

SELECT DISTINCT * FROM (your union queries)
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

The first two articles in this short series — Using a Criteria Form to Filter Records (http://www.experts-exchange.com/A_6069.html) and Building a Custom Filter (http://www.experts-exchange.com/A_6070.html) — discuss in some detail how a form can be…
QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
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.

728 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now