如何从多列中获取计数?

huangapple go评论74阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年3月8日 19:10:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75672240.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定