简体   繁体   English

sql总和特定计数值

[英]sql sum specific counted values separately

I am trying to make a sql script that count the number of customers grouped by branch and it worked, but I want to divide the counted branches into region and sum every region, like this: 我正在尝试制作一个sql脚本,该脚本可以对按分支分组的客户数量进行计数,并且可以正常工作,但是我想将计数的分支划分为区域,并对每个区域求和,如下所示:

  numberofcustomer |  branch
-------------------+-----
                 3 | branch1-1
                 4 | branch1-2
                 7 | region1
                 3 | branch2-1
                 3 | branch2-2
                 2 | branch2-3
                 5 | region2

here is my code: 这是我的代码:

select Branch_name,
COUNT(customer.customerID) as numberofcustomer 
from customer inner join Branch
on customer.BranchID = Branch.BranchID 
group by Branch_name
ORDER By Branch_name

here is the result of my code: 这是我的代码的结果:

  numberofcustomer | branch
-------------------+-----
                 3 | branch1-1
                 4 | branch1-2
                 3 | branch2-1
                 3 | branch2-2
                 2 | branch2-3

为什么不为区域创建另一个表,为区域创建表,在分支表中已经添加了区域ID作为fk,例如,在两个表之间建立内部联接,例如在查询中,在两个表之间进行内部联接A和客户以及按地区名称分组

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM