Praveen S
Praveen S

Reputation: 650

How to combine three Select Statements using UNION

I used following query for combine to get different results.I combined first two select queries I need to combine third select query with previous two queries. Suggest your ideas please.

My Query is:

Select distinct Emp.EmpID,Emp.HolderName,jobprf.pkprofileid,
                    CASE WHEN jobprf.isSelfEvaluvated=1 THEN 'EVAL' ELSE 'NOTEVAL' END as Evalue,
                    CASE WHEN jobprf.isSelfEvaluvated=0  THEN 'Self Evaluvation Pending' ELSE 'Self Evaluvated' END as selfeval,
                    CASE WHEN jobprf.isMgEvaluvated=0 THEN 'Manger Evaluvation Pending' ELSE 'Manager Evaluvated' END as mangeval
         from tblEmpRegistration Emp inner join  tbljobprofile jobprf on Emp.EmpID=jobprf.EmpID  where Emp.EmpID=@Mangrid or Emp.ManagerID=@Mangrid  and jobprf.RaterID<>@Mangrid 

        UNION All

        Select distinct Emp.EmpID,Emp.HolderName,Emp.CompanyID,Emp.HolderName,
            CASE WHEN Emp.EmpID IS NOT NULL THEN 'Self Evaluvation Pending' ELSE 'Self Evaluvated' END as selfeval,
            CASE WHEN Emp.EmpID IS NOT NULL THEN 'Manger Evaluvation Pending' ELSE 'Manager Evaluvated' END as mangeval
        from tblEmpRegistration Emp where Emp.EmpID NOT IN(SELECT EmpID FROM tbljobprofile) AND Emp.TITLE<>2 order by Emp.EmpID

        UNION All

        Select pkprofileid,isSelfEvaluvated,isMgEvaluvated,isMgEvaluvated,isMgEvaluvated,isMgEvaluvated from tbljobprofile where RaterID=@Mangrid 

And it shows an error

Incorrect syntax near the keyword 'UNION'

Upvotes: 0

Views: 3345

Answers (2)

Hamlet Hakobyan
Hamlet Hakobyan

Reputation: 33381

You can't use ORDER BY clause in your UNION clause.

Upvotes: 4

Deepak Sharma
Deepak Sharma

Reputation: 4170

you can union if all the select queries return the same set for column in same order..!! and here in your case column set and order are not same, so it will throw an error.

Upvotes: 1

Related Questions