Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
SQL
(SELECT sp.PK as swithPersonId, sp.PTPK, emp.Employee_Name, emp.PK, pro.Name, sp.SwitchDate, sp.Half from switch_person sp
            INNER JOIN projects pro
            ON sp.PTPK = pro.PK
            INNER JOIN employee emp
            ON sp.EmployeeNamePK = emp.PK
            WHERE pro.TeamLead = 33
            AND sp.SwitchDate = '2015-6-25'
            AND sp.Half IN (1,2)
            AND sp.LeadBy <> sp.EmployeeNamePK
            ORDER BY pro.PK,emp.PK, sp.Half)



SQL
(SELECT sp.PK as swithPersonId, sp.PTPK, emp.Employee_Name, emp.PK, pro.Name,        sp.SwitchDate, sp.Half from switch_person sp
 INNER JOIN projects pro
 ON sp.PTPK = pro.PK
 INNER JOIN employee emp
 ON sp.EmployeeNamePK = emp.PK
 WHERE sp.SwitchDate = '2015-6-25'
 AND sp.Half IN (1,2)
 AND sp.LeadBy = sp.EmployeeNamePK
 ORDER BY pro.PK,emp.PK, sp.Half )

these two query I am going to combine but I want that order of the result should not change. means first query order and second query order don't mingled. using this and it happening ;(
SQL
(SELECT sp.PK as swithPersonId, sp.PTPK, emp.Employee_Name, emp.PK, pro.Name, sp.SwitchDate, sp.Half from switch_person sp
            INNER JOIN projects pro
            ON sp.PTPK = pro.PK
            INNER JOIN employee emp
            ON sp.EmployeeNamePK = emp.PK
            WHERE pro.TeamLead = 33
            AND sp.SwitchDate = '2015-6-25'
            AND sp.Half IN (1,2)
            AND sp.LeadBy <> sp.EmployeeNamePK
            ORDER BY pro.PK,emp.PK, sp.Half)
            union all
           (SELECT sp.PK as swithPersonId, sp.PTPK, emp.Employee_Name, emp.PK, pro.Name, 		sp.SwitchDate, sp.Half from switch_person sp
            INNER JOIN projects pro
            ON sp.PTPK = pro.PK
            INNER JOIN employee emp
            ON sp.EmployeeNamePK = emp.PK
            WHERE sp.SwitchDate = '2015-6-25'
            AND sp.Half IN (1,2)
            AND sp.LeadBy = sp.EmployeeNamePK
            ORDER BY pro.PK,emp.PK, sp.Half )



how to maintain the order of both query separately
Posted

All you need to do is add another field onto your queries. For example:
SQL
SELECT field1, field2, ... 1 AS OrderBy
UNION ALL
SELECT field1, field2, ... 2 AS OrderBy
ORDER BY OrderBy


That should get you through it.
 
Share this answer
 
Comments
Muhamad Faizan Khan 26-Jun-15 8:00am    
did not work
ZurdoDev 26-Jun-15 8:15am    
It will work so you must have done something different.
Add a constant to your query which will be unique for the query:
SQL
SELECT 1 as queryId, sp.PK as swithPersonId, ...
UNION ALL
SELECT 2 as queryId, sp.PK as swithPersonId, ...

ORDER BY queryId, swithPersonId, PK, Half

This way the results of the first query will always come before the results of the second query.

Edit - complete query
SQL
with cte as (
	select sp.PK as swithPersonId, sp.PTPK, emp.Employee_Name, emp.PK, pro.Name, sp.SwitchDate, sp.Half, sp.LeadBy, sp.EmployeeNamePK
	from switch_person sp
	INNER JOIN projects pro	ON sp.PTPK = pro.PK
	INNER JOIN employee emp	ON sp.EmployeeNamePK = emp.PK
	where pro.TeamLead = 33
		AND sp.SwitchDate = '2015-6-25'
		AND sp.Half IN (1,2)
)
, cte2 as
(
	select 1 as queryId, swithPersonId, PTPK, Employee_Name, PK, Name, SwitchDate, Half
	from cte where LeadBy <> EmployeeNamePK

	union all

	select 2 as queryId, swithPersonId, PTPK, Employee_Name, PK, Name, SwitchDate, Half
	from cte where LeadBy = EmployeeNamePK
)
select swithPersonId, PTPK, Employee_Name, PK, Name, SwitchDate, Half
from cte2
order by queryId, swithPersonId, PK, Half

First CTE gets the data you need. Second CTE separates the two queries based on LeadBy value where all records with "LeadBy <> EmployeeNamePK" will get before any records where "LeadBy = EmployeeNamePK". Finally we only select those columns which we are interested in and order the results.
 
Share this answer
 
v2
Comments
Muhamad Faizan Khan 26-Jun-15 8:06am    
i use this
SELECT 1 as queryId, sp.PK as swithPersonId, sp.PTPK, emp.Employee_Name, emp.PK, pro.Name, sp.SwitchDate, sp.Half from switch_person sp
INNER JOIN projects pro
ON sp.PTPK = pro.PK
INNER JOIN employee emp
ON sp.EmployeeNamePK = emp.PK
WHERE pro.TeamLead = 33
AND sp.SwitchDate = '2015-6-25'
AND sp.Half IN (1,2)
AND sp.LeadBy <> sp.EmployeeNamePK
union all
SELECT 2 as queryId, sp.PK as swithPersonId, sp.PTPK, emp.Employee_Name, emp.PK, pro.Name, sp.SwitchDate, sp.Half from switch_person sp
INNER JOIN projects pro
ON sp.PTPK = pro.PK
INNER JOIN employee emp
ON sp.EmployeeNamePK = emp.PK
WHERE sp.SwitchDate = '2015-6-25'
AND sp.Half IN (1,2)
AND sp.LeadBy = sp.EmployeeNamePK

ORDER BY queryId, swithPersonId, PK, Half


but it didnt work
Tomas Takac 26-Jun-15 8:25am    
Your query is ok. What exactly do you mean by "it didn't work"?
Muhamad Faizan Khan 26-Jun-15 9:11am    
means it not maintaining the order
Tomas Takac 26-Jun-15 10:25am    
I updated the solution with complete query. If the ordering is not correct I suggest you update your question with a sample input and desired output.
The query should be something like this:

Select swithPersonId, PTPK, Employee_Name, PK, Name, SwitchDate, Half
from

(
SELECT sp.PK as swithPersonId, sp.PTPK, emp.Employee_Name, emp.PK, pro.Name, sp.SwitchDate, sp.Half, 1 as filter
from switch_person sp
            INNER JOIN projects pro
            ON sp.PTPK = pro.PK
            INNER JOIN employee emp
            ON sp.EmployeeNamePK = emp.PK
            WHERE pro.TeamLead = 33
            AND sp.SwitchDate = '2015-6-25'
            AND sp.Half IN (1,2)
            AND sp.LeadBy <> sp.EmployeeNamePK
            ORDER BY pro.PK,emp.PK, sp.Half

union all

SELECT sp.PK as swithPersonId, sp.PTPK, emp.Employee_Name, emp.PK, pro.Name, sp.SwitchDate, sp.Half, 2 as filter
from switch_person sp
            INNER JOIN projects pro
            ON sp.PTPK = pro.PK
            INNER JOIN employee emp
            ON sp.EmployeeNamePK = emp.PK
            WHERE sp.SwitchDate = '2015-6-25'
            AND sp.Half IN (1,2)
            AND sp.LeadBy = sp.EmployeeNamePK
            ORDER BY pro.PK,emp.PK, sp.Half
)

order by filter
 
Share this answer
 
Comments
Muhamad Faizan Khan 26-Jun-15 8:07am    
#1221 - Incorrect usage of UNION and ORDER BY
Muhamad Faizan Khan 26-Jun-15 8:07am    
it is very bad practice to share non executed code
Sreekanth Mothukuru 26-Jun-15 8:14am    
We are trying to replicate the solution near to your query. Try to fix any syntax for the given solution as we don't have access to your SSMS
Muhamad Faizan Khan 26-Jun-15 9:09am    
i have mention the error it is related to syntax

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900