0

我有以下问题:

表#1结构:

例如,请求表有 2 列:

Request_ID 分支名称

1                  xx 
2                  yy
3                  xx
4                  xx
5                  yy

第二个表:Requests_Items 例如有以下列

Request_ID      Price  

1               100   
1               30
1               450
2               10
2               18

所以我希望我的结果集是这样的:

 Branch Name     Num_of_Requests     Total_Price 

    xx                 3               580

你能帮忙吗

4

4 回答 4

2
select r.Branch_Name, 
       count(r.request_id) as Num_of_Requests, 
       sum(i.price) as Total_Price 
from Requests r
left join Requests_Items i on i.Request_ID = r.Request_ID  
group by r.Branch_Name
于 2013-10-22T13:33:50.167 回答
0

jurgen d 快到了

select      r.Request_Id, r.Branch_Name,
            COUNT(i.Price) as [Number of Requests],
            SUM(i.Price) as [Total]
from        Requests r
left join   Requests_Items i
on          r.Request_Id = i.Request_Id
group by    r.Request_Id, r.Branch_Name
于 2013-10-22T14:27:19.180 回答
0
select r.Branch_Name, COUNT(ri.Request_ID) as Num_of_Requests, SUM(ri.Price) as Total_Price
from dbo.Requests r
left join dbo.Requests_Items ri
on r.Request_ID = ri.Request_ID
group by r.Branch_Name
于 2013-10-22T14:28:31.267 回答
0

让这是我的[Employees ] 表

在此处输入图像描述

这是我的部门

在此处输入图像描述

我想要这样的输出使用join 和 group by

在此处输入图像描述

所以这里是这个查询 -

select max(e.salary) as maxsalary,d.Dept from Employees e inner join DEpt d on e.DeptId=d.id group by e.DeptId,d.Dept
    enter code here
于 2016-11-18T11:51:22.843 回答