首页 > 其他分享 >07.聚合函数

07.聚合函数

时间:2022-10-16 21:57:05浏览次数:59  
标签:聚合 07 PeopleSalary 函数 People PeopleBirth year select getdate

聚合函数

sql中主要聚合函数有

count  求数量
max    求最大值
min    求最小值
sum    求和
avg    求平均值
--求员工总人数
select count(*) 总人数 from People
--求员工的最高工资
select max(PeopleSalary) 最高工资 from People
--求员工的最低工资
select min(PeopleSalary) 最低工资 from People 
--求所有员工的工资总和
select sum(PeopleSalary) 工资的和 from People
--求所有员工的平均工资
select round(avg(PeopleSalary),0) 平均工资 from People
--求数量,最大值,最小值,总和,平均值,在一行显示
select count(*) 总人数,max(PeopleSalary) 最高工资,
min(PeopleSalary) 最低工资,sum(PeopleSalary) 工资的和,
round(avg(PeopleSalary),0) 平均工资 from People
--查询出武汉地区的员工人数,总工资,最高工资,最低工资和平均工资
select count(*) 总人数,max(PeopleSalary) 最高工资,
min(PeopleSalary) 最低工资,sum(PeopleSalary) 工资的和,
round(avg(PeopleSalary),0) 平均工资 from People
where PeopleAddr='武汉'
--查询出工资比平均工资高的的人员信息
select*from People where PeopleSalary >
(select avg(PeopleSalary) from People)
--求数量,年龄最大值,年龄最小值,年龄总和,年龄平均值
--方案1
select count(*) 数量,max(year(getdate())-year(PeopleBirth)) 最大年龄,
min(year(getdate())-year(PeopleBirth)) 最小年龄, 
sum(year(getdate())-year(PeopleBirth)) 年龄总和,
avg(year(getdate())-year(PeopleBirth)) 平均年龄
from People
--方案2
select count(*) 数量,
max(datediff(year,PeopleBirth,getdate())) 最大年龄,
min(datediff(year,PeopleBirth,getdate())) 最小年龄,
sum(datediff(year,PeopleBirth,getdate())) 年龄总和,
avg(datediff(year,PeopleBirth,getdate())) 平均年龄
from People
--计算出月薪1000以上的男性的最大年龄,最小年龄和平均年龄
select '月薪>1000' 月薪,'男' 性别, 
max(datediff(year,PeopleBirth,getdate())) 最大年龄,
min(datediff(year,PeopleBirth,getdate())) 最小年龄,
avg(datediff(year,PeopleBirth,getdate())) 平均年龄
from People where PeopleSalary>1000 and PeopleSex='男'
--统计出所在地在'武汉或上海'的所有女员工数量及最大年龄,最小年龄和平均工资
select '武汉或上海的女员工' 描述, 
count(*) 数量,
max(datediff(year,PeopleBirth,getdate())) 最大年龄,
min(datediff(year,PeopleBirth,getdate())) 最小年龄,
sum(datediff(year,PeopleBirth,getdate())) 年龄总和,
avg(datediff(year,PeopleBirth,getdate())) 平均年龄
from People where PeopleSex='女' and PeopleAddr in('武汉','北京')
--求出年龄比平均年龄高的人员信息
select*from People where datediff(year,PeopleBirth,getdate()) >
(select avg( year(getdate()) - year(PeopleBirth) ) from People )

标签:聚合,07,PeopleSalary,函数,People,PeopleBirth,year,select,getdate
From: https://www.cnblogs.com/rain-blogs/p/16797310.html

相关文章

  • 【2022 CCPC Henan Provincial Collegiate Programming Contest #K】复合函数
    题目链接K.复合函数输入文件:standardinput输出文件:standardoutput时间限制:1second空间限制:512megabytes给定正整数\(n\),并记\(I_n={1,2,\cdots,n}\)。给定......
  • [HNOI2007]紧急疏散EVACUATE
    题目传送门:[HNOI2007]紧急疏散EVACUATEbfs+二分+最大流#include<queue>#include<string>#include<cstdio>#include<cstring>#include<algorithm>constintN=2......
  • 【数据结构】栈的定义以及接口函数的C语言代码实现(仅供学习交流使用)
    1、栈的定义栈:一种特殊的线性表,其只允许在固定的一端进行插入和删除元素操作。进行数据插入和删除操作的一端称为栈顶,另一端称为栈底。栈中的数据元素遵守后进先出LIFO(Last......
  • Python函数
    5.1函数相关基础概念5.1.1函数是什么函数是指一段可以直接被另一段程序或代码引用的程序或代码。也叫做子程序、(OOP中)方法。一个较大的程序一般应分为若干个程序块,每......
  • 函数知识的回顾
    内容回顾1.文件文件的基本操作什么是文件? 文件是操作系统暴露给用户操作硬盘的快捷方式文件打开的固定模板 withopen(r'a.txt',r,encoding='utf8')asfwith......
  • 常见内置函数
    目录常见内置函数一、重要内置函数1、map()——映射2、max()和min()3、reduce()4、zip5、filter6、sorted二、常见内置函数(了解)1、abs2、all3、any4、bytes5、bin、oct、he......
  • 闭包函数
    目录闭包函数一、基础知识二、作用闭包函数一、基础知识所谓闭包函数就是定义在函数内部的函数,但是他有一些限制条件:1、定义在函数内部2、用到了外部函数名称空间中......
  • 递归函数
    目录递归函数递归函数递归函数就是直接或间接调用函数自身的函数,当我们使用这种函数的时候,并不会出现预料之中的死循环,当循环次数达到1000左右就会被解释器强行停止,虽然......
  • 函数
    目录函数一、函数基础知识1、概念讲解2、语法结构3、函数的定义与调用4、函数的分类5、函数的返回值二、函数参数1、位置参数位置形参位置实参2、默认参数(关键字参数)关键字......
  • 07:有趣的跳跃
    描述一个长度为n(n>0)的序列中存在“有趣的跳跃”当前仅当相邻元素的差的绝对值经过排序后正好是从1到(n-1)。例如,1423存在“有趣的跳跃”,因为差的绝对值分别为3,2,1。当......