英文:
How could I obtain a count from multiple colums?
问题
我有一个在SQL Server中的表,其中包含有关课程(Courses)、工作区域(WorkingArea)和省份(Province)的数据,我想获取每个省份和每个课程的工作区域计数。
例如,对于“Course A”在“Almeria”有2个“Desempleados”,所以我想在另一列中得到“2”作为结果,但是当我尝试使用GROUP BY时,结果总是不正确...
结果应该如下:
课程名称 | 工作区域 | 省份 | 计数 |
---|---|---|---|
COURSE A | Desempleado | Almería | 2 |
COURSE A | Administración Autonómica | Almería | 1 |
英文:
I have a table in SQL server with data of Courses, WorkingArea and Province and I want to get the count of WorkingArea for each Province and for each course.
Could you help me please?
For example, there are 2 "Desempleados" in "Almeria" for "Course A", so I want "2" as result in another column, but when I try to group by, the results are never ok...
The result should be:
CourseName | WorkingArea | Province | Count |
---|---|---|---|
COURSE A | Desempleado | Almería | 2 |
COURSE A | Administración Autonómica | Almería | 1 |
答案1
得分: 2
你使用了GROUP BY
子句。GROUP BY
子句用在SELECT
语句内,根据一个或多个列的值将行分组为集合。通常与聚合函数一起使用,如SUM、AVG、MAX、MIN和COUNT,用于计算每个组的汇总信息。例如:
SELECT
课程名称
, 工作区域
, 省份
, count(*) AS 计数
FROM 你的表格
GROUP BY
课程名称
, 工作区域
, 省份
英文:
You use a GROUP BY
clause. The GROUP BY
clause is used within a SELECT
statement to group rows into sets based on the values of one or more columns. It is often used with aggregate functions such as SUM, AVG, MAX, MIN, and COUNT to calculate summary information for each group. e.g.
SELECT
coursename
, workingarea
, province
, count(*) AS cnt
FROM your_table
GROUP BY
coursename
, workingarea
, province
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论