Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

merging 2 queries

Status
Not open for further replies.

shopwise

Technical User
Joined
Sep 22, 2008
Messages
52
Location
US
I have 2 separate queries:

a Union query that I've named "UNION" and

a "find duplicates for UNION" query which access has generated for me.

Now that the underlying data for UNION has grown in size, the "find
duplicates for UNION query" freezes when attempting to open it.

The remedy is most likely to incorporate the SQL code of the "find
duplicates for UNION" query into the SQL code for UNION so that the 2 queries
are merged.

How would I accomplish this using the below queries code:

UNION query SQL code:

(SELECT DocumentHeaders.SoldToFax,First( DocumentHeaders.SoldToCompany) As
FCompany, First(DocumentHeaders.DataSource) As FDataSource
,DocumentHeaders.Remove, First(DocumentHeaders.catagoryID) As FCatID,
Min(DocumentHeaders.ID) As MID
FROM DocumentHeaders
GROUP BY DocumentHeaders.SoldToFax, DocumentHeaders.Remove
HAVING (((DocumentHeaders.SoldToFax)<>"" ) AND ((DocumentHeaders.Remove)<>-1))
UNION
SELECT DocumentHeaders.ShipToFax, First(DocumentHeaders.ShipToCompany) As
FCompany, First(DocumentHeaders.DataSource) As FDataSource,
DocumentHeaders.Remove, First( DocumentHeaders.catagoryID) As FCatID,
Min(DocumentHeaders.ID) As MID
FROM DocumentHeaders
WHERE DocumentHeaders.Remove <>-1
GROUP BY DocumentHeaders.ShipToFax, DocumentHeaders.Remove
HAVING (((DocumentHeaders.ShipToFax)<>"" )))
UNION (SELECT faxnumbers.SoldToFax, faxnumbers.MSoldToCompany,
faxnumbers.DataSource, faxnumbers.Remove, faxnumbers.CatagoryID, faxnumbers.ID
FROM faxnumbers WHERE faxnumbers.Remove<>-1);



"find duplicates for UNION" SQL code:

SELECT UNION.SoldToFax, UNION.FCompany, UNION.FDataSource, UNION.Remove,
UNION.FCatID, UNION.MID
FROM [UNION]
WHERE (((UNION.SoldToFax) In (SELECT [SoldToFax] FROM [UNION] As Tmp GROUP
BY [SoldToFax] HAVING Count(*)>1 )))
ORDER BY UNION.SoldToFax;


thanks in advance!
 
SELECT .[SoldToFax] , u.FCompany , u.FDataSource , u.Remove , u.FCatID , u.MID
FROM UNIO INNER JOIN [SELECT SoldToFax FROM UNIO As Tmp GROUP
BY SoldToFax HAVING Count(*)>1 ]. AS DupSoldToFax ON UNIO.Soldtofax = DupSoldToFax.SoldToFax;


bad idea to name query union
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top