troubleshooting Question

Syntax Error in Union Query

Avatar of ptslv
ptslvFlag for United States of America asked on
Microsoft SQL ServerMicrosoft SQL Server 2008
5 Comments1 Solution469 ViewsLast Modified:
Can anyone tell me why I am getting a message:  "Syntax error ')' near GROUP BY "?

My output should look like:

TaskID               Formname              Recordcount
    1                    Form 1                            3
    1                    Form 2                            1
    2                    Form 1                            4
    2                    Form 2                            0

Here is my query:

" SELECT   MsnID, RecordCount, Formname " +
                        " FROM " +
                        " (SELECT   TaskID, Count(RecNum) as RecordCount, 'Form 1'  AS Formname " +
                        "  FROM   tbl_Table1 " +
                        " UNION " +
                        " SELECT  TaskID,  Count(RecNum) as RecordCount, 'Form 2' AS Formname " +
                        " FROM    tbl_Table2) " +
                        " GROUP BY TaskID, Formname ";
ASKER CERTIFIED SOLUTION
Aneesh
Database Consultant
Join our community to see this answer!
Unlock 1 Answer and 5 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 5 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros