需要明智地选择员工部门的平均年龄(Need to select average age of employees department wise)
我在Postgresql DB中有一个表。 它有3个字段:Emp_Name,Dept和Age。 我需要明智地显示员工部门的平均年龄。 我需要在结果集中显示所有3个字段。 以下是输入和预期输出:
这是SQL小提琴: http ://sqlfiddle.com/#!15/ 7c4cf
如何在PostgreSQL中显示预期结果?
I have a table in Postgresql DB. It has 3 fields: Emp_Name, Dept & Age. I need to show Average age of employees department wise. I need to display all 3 fields in the result-set. Below is the input and expected output:
Here is the SQL Fiddle : http://sqlfiddle.com/#!15/7c4cf
How do I show the expected result in PostgreSQL?
最满意答案
您可以使用string_agg函数来合并员工姓名,并使用avg来获得平均年龄:
SELECT STRING_AGG(emp_name, ','), dept, AVG(age) FROM test1 GROUP BY deptSQLFiddle
You can use the string_agg function to concatinate the employee names and avg to get the average age:
SELECT STRING_AGG(emp_name, ','), dept, AVG(age) FROM test1 GROUP BY deptSQLFiddle
更多推荐
发布评论