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 syntax problem (alias issue?)

Status
Not open for further replies.

Hinfer

Programmer
Sep 14, 2001
69
0
0
CA
The code below (see code that doesn't work) gives me a error "syntax error in union". Once I click ok it highlight the bracket in ") as Q1".

The strange thing is if I break this query apart then it works. There must be something wrong with my syntax surrounding the aliasing. I would like to keep it together if I can. Any help would be appreciated.
Code:
//code that doesn't work
SELECT 
AssignmentPositions.Employee
FROM
[
SELECT Q1.Employee, Q1.StartDate, Min(Q1.EndDate) AS EndDate
    FROM (
              SELECT Position.Employee as Employee,  Position.StartDate as StartDate, Position.EndDate as EndDate
              FROM [Position] 
             UNION ALL
             SELECT Assignment.Employee as Employee, Assignment.StartDate as StartDate, Assignment.EndDate as EndDate
             FROM Assignment
             ) as Q1
   GROUP BY Q1.Employee, Q1.StartDate
   ORDER BY Q1.StartDate
]. AS AssignmentPositions

Code:
//Code that works
SELECT Position.Employee as Employee,  Position.StartDate as StartDate, Position.EndDate as EndDate
              FROM [Position] 
             UNION ALL
             SELECT Assignment.Employee as Employee, Assignment.StartDate as StartDate, Assignment.EndDate as EndDate
             FROM Assignment
             ) as Q1
   GROUP BY Q1.Employee, Q1.StartDate
   ORDER BY Q1.StartDate
 
The query builder does that to your raw SQL when you paste it in there. It puts the inner Select into a [ ]. construct instead of leaving it in ( )

Your first query will work if you replace the '[' after the FROM with '(' and the '].' before 'AS AssignmentPositions' with ')'

 
Thanks again. (I should have tried that - d'oh)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top