Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Need Help in a Query.

Author  Topic 

aashirwaads
Starting Member

8 Posts

Posted - 2007-08-22 : 06:43:36
Hi friends,

Please guide me as I am stuck in a query

I have got 3 queries

1.columns-employee_id,employee_name,(employees with commission)
2.columns-employee_id,employee_name,(employees without commission)
3.columns-employee_id,employee_name,total


now I want to join these three queries so that the output is shown as one table as below

employee_id,employee_name,(employees with commission),(employees without commission),total

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-08-22 : 07:04:13
This can almost cenrtainly be done in only ONE query!
If you post the DDL you can sort something out.
This maybe?
SELECT		Employee_ID,
Employee_Name,
SUM(Employees_with_Commission) AS Employees_with_Commission,
SUM(Employees_Without_Commission) AS Employees_Without_Commission,
SUM(Total) AS Total
FROM (
SELECT Employee_ID, Employee_Name, Employees_with_Commission, 0 AS Employees_Without_Commission, 0 AS Total FROM Query1 UNION ALL
SELECT Employee_ID, Employee_Name, 0, Employees_Without_Commission, 0 FROM Query2 UNION ALL
SELECT Employee_ID, Employee_Name, 0, 0, Total FROM Query3
) AS d
GROUP BY Employee_ID,
Employee_Name
ORDER BY Employee_ID,
Employee_Name



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page
   

- Advertisement -