SQL查询组从两个表中的数据 [英] Sql Query to group the data from two tables
本文介绍了SQL查询组从两个表中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个表处
和员工
。
处表如下所示:
ID DeptName
1 IT
2 CSE
3 ECE
员工表:
ID DeptID EmployeeName Salary
1 1 John 10000
2 1 Bob 15000
3 2 Akon 12000
4 2 Smith 20000
现在我要组以这样的方式,我得到下面的结果,其中包括这些列中的数据:
Now I want to group the data in such a way that I get the following results which include these columns :
ID DeptName Employee
1 IT John,10000
Bob,15000
2 CSE Akon,12000
Smith,20000
我们可以做这样的事情使用SQL组函数或任何其他方式?
Can we do something like this using SQL group functions or any other way?
请帮我。
谢谢,
Rajbir
Thanks, Rajbir
推荐答案
这
select final.deptId, d.deptName,
e3.employeename + ',' + cast(e3.salary as varchar) employee
from employee e3
left join (
select e1.id, e1.deptId from employee e1
left join employee e2
on e1.deptId = e2.deptId and e1.id > e2.id
where e2.id is null
) final on e3.id = final.id
left join department d on d.id = final.deptId
结果:
+--------+----------+-------------+
| DEPTID | DEPTNAME | EMPLOYEE |
+--------+----------+-------------+
| 1 | IT | John,10000 |
| | | Bob,15000 |
| 2 | CSE | Akon,12000 |
| | | Smith,20000 |
+--------+----------+-------------+
请注意,空白的价值观实际上是充满了空
值。
让我知道如果你有任何问题吧。
Let me know if you have any issue with it.
这篇关于SQL查询组从两个表中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文