需要明智地选择员工部门的平均年龄(Need to select average age of employees department wise)

编程入门 行业动态 更新时间:2024-10-10 03:22:40
需要明智地选择员工部门的平均年龄(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 dept

SQLFiddle

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 dept

SQLFiddle

更多推荐

本文发布于:2023-08-04 17:07:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1419235.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:明智   平均   年龄   员工   部门

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!