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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Union Query Several queries with parameters

Status
Not open for further replies.

summer01

Technical User
Jan 28, 2008
19
US
I am new to Union Queries and struggling with how to format one that combines about 6 different queries with duplicate field names and contains parameters. I am hoping someone can assist so I can learn better how to do these. Below are are the SQL's for 2 of the 6 queries so you can see how they are formatted. I have looked at several sources and cannot seem to get anything to work.

SELECT Count([BC Facility Closed].[Closed Dt]) AS [CountOfClosed Dt], [BC Facility Closed].[Closed Dt]
FROM [BC Facility Closed]
WHERE ((([Closed Dt]-[Recd Dt])>=0 And ([Closed Dt]-[Recd Dt])<=7))
GROUP BY [BC Facility Closed].[Closed Dt];


SELECT Count([BC Facility Closed].[Closed Dt]) AS [CountOfClosed Dt], [BC Facility Closed].[Closed Dt]
FROM [BC Facility Closed]
WHERE ((([Closed Dt]-[Recd Dt])>=8 And ([Closed Dt]-[Recd Dt])<=21))
GROUP BY [BC Facility Closed].[Closed Dt];


 
And the problem is ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
For starters, a UNION query generally includes the word UNION. You don't have one here.
Code:
SELECT Count([Closed Dt]) AS [CountOfClosed Dt], [Closed Dt]
FROM [BC Facility Closed]
WHERE [Closed Dt]-[Recd Dt] >= 0 
  And [Closed Dt]-[Recd Dt] <= 7
GROUP BY [Closed Dt]

UNION 

SELECT Count([Closed Dt]) AS [CountOfClosed Dt], [Closed Dt]
FROM [BC Facility Closed]
WHERE [Closed Dt]-[Recd Dt] >= 8 
  And [Closed Dt]-[Recd Dt] <= 21
GROUP BY [Closed Dt]

A union query takes its field names from the first SELECT in the Union. The other Select statements must have the same number of fields of the same data types but their names are ignored.

The Union illustrated will eliminate duplicate records (if any) in the result. Use UNION ALL if you want to retain duplicates.

I don't see any parameters in the code you posted.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top