首页数据库数据库avg,ACCESS数据库求平均值

数据库avg,ACCESS数据库求平均值

编程之家2023-10-1880次浏览

大家好,今天来为大家分享数据库avg的一些知识点,和ACCESS数据库求平均值的问题解析,大家要是都明白,那么可以忽略,如果不太清楚的话可以看看本篇文章,相信很大概率可以解决您的问题,接下来我们就一起来看看吧!

数据库avg,ACCESS数据库求平均值

ACCESS数据库求平均值

在Access数据库中对数据表求某字段平均值用的是AVG函数,具体操作如下:

1、打开Access,准备一个数据表,对Age列求平均数,如下图所示:

2、点击顶部的查询向导,如下图所示:

3、新建查询界面选择简单查询向导,如下图所示:

4、右键单击查询标题,选择SQL视图,如下图所示:

5、输入SELECT Avg(表1.[Age]) FROM表1语句,如下图所示:

数据库avg,ACCESS数据库求平均值

6、执行SQL语句就可得到Age列的平均数了,如下图所示:

数据库高级查询2

分组查询(group by)

分组查询:

1.查询每个部门的最高工资

数据库avg,ACCESS数据库求平均值

select deptno,max(sal) from emp group by deptno;

2.查询每个职位的平均工资

select deptno,avg(sal) from emp group by deptno;

3.查询每个部门的人数

select deptno,count(*) from emp group by deptno;

4.查询工资大于1000的员工,每个部门的最大工资

select deptno,max(sal) from emp where sal>1000 group by deptno;

多字段分组查询:group by字段1,字段2;

1.查询每个部门下每个主管的手下人数

select deptno, mgr, count(*) from emp where mgr is not null group by deptno,mgr;

2.查询emp表中每个部门的编号,人数,工资总和,最后根据人数进行升序排序,如果人数一致,根据工资总和降序排序

select deptno,count(*),sum(sal) from emp group by deptno order by count(*) asc,sum(sal) desc;

3.查询工资在1000-3000之间的员工信息,每个部门的编号,平均工资,最低工资,最高工资,根据平均工资进行升序排序排列

select deptno,avg(sal),min(sal),max(sal) from emp where sal between 1000 and 3000 group by deptno order by avg(sal)

4.查询含有上级领导的员工,每个职业的人数,工资的总和,平均工资,最低工资,最后根据人数进行降序排列,如果人数一致,根据平均工资进行升序排列

select job, count(*),avg(sal),min(sal) from emp where mgr is not null group by job order by count(*) desc,avg(sal) asc;

各种关键字的顺序

select* from表名  where.... group...having... order by... limit...

   having(结合group by使用)

having一般要结合分组查询和聚合函数使用,用于给聚合函数的内容添加条件

聚合函数的条件不能写在where后面

普通字段的条件写在where后面,聚合函数的条件写在having后面

1.查询每个部门的平均工资,要求平均工资大于2000(c是别名的用法)

select deptno,avg(sal) c from emp group by deptno having c>2000;

2.查询每个分类的平均单价,要求平均单价低于100

select category_id,avg(price) a from t_item group by category_id having a<100;

3.查询category_id分类为238和917的两个分类的平均单价

select category_id,avg(price) from t_item where category_id in(238,917) group by category_id;

4.查询emp表中每个部门的平均工资高于2000的部门编号,部门人数,平均工资,最后根据平均工资降序排列

select deptno,count(*),avg(sal) a from emp group by deptno having a>2000 order by a desc;

5.查询emp表中工资在1000-3000之间的员工,每个部门编号,工资总和,平均工资,过滤掉平均工资低于2000的部门,按照平均工资进行升序排序

select deptno,sum(sal), avg(sal) a from emp where sal between 1000 and 3000 group by deptno having a>=2000 order by a asc;

6.查询emp表中每年入职的人数

select extract(year from hiredate) year,count(*) from emp group by year;

7.查询每个部门的最高平均工资

select deptno,avg(sal) from emp group by deptno order by avg(sal) limit 0,1;

子查询(嵌套查询)

子查询可以写在where或having后面当做查询条件的值

写在from后面,当做一张新表(但是必须要有别名)

select ename from(select* from emp where sal>1000) newtable;

写在创建表的时候

create table emp_20 as(select* from emp where deptno=20);

1.查询emp表中工资最高的员工信息

select* from emp where sal=(select max(sal) from emp);

2.查询emp表中工资大于平均工资的所有员工的信息

select* from emp where sal>(select avg(sal) from emp);

3.查询工资高于20号部门最大工资的员工信息

select* from emp where sal>(select max(sal) from emp where deptno=20);

4.查询工资高于20号部门最大工资的员工信息

select* from emp where sal>(select avg(sal) from emp);

5.查询和Jones相同工资的其他员工信息

select* from emp where job=(select job from emp where ename='jones' and ename!='jones');

6.查询工资最低的员工的同事们的信息(同事=相同job)

select* from  emp where job=(select job from emp where sal=(select min(sal) from emp)) and sal!=(select min(sal) from emp);

7.查询最晚入职的员工信息

select* from emp where hiredate=(select max(hiredate) from emp);

8.查询名字为King的部门编号和部门名称(需要用到dept表)

select deptno,dname from dept where deptno=(select deptno from emp where ename='king');

9.查询有员工的部门信息(编号和名称)

select deptno,dname from dept where deptno in(select distinct deptno from emp);

10.查询平均工资最高的部门信息

select* from dept where deptno in(select deptno from emp group by deptno having avg(sal)=(select avg(sal) from emp group by deptno order by avg(sal) desc limit 0,1));

关联查询

同时查询多张表的数据称为关联查询

1.查询每一个员工的名称和其对应的部门名称

select e.ename,d.dname from emp e,dept d where e.deptno=d.deptno;

2.查询在new york工作的所有员工的信息

select  e.* from emp  e,dept  d  where e.deptno=d.deptno and  d.loc='new york';

笛卡尔积

如果关联查询不写关联关系则查询到的数据是两张表的乘积,这个乘积称为笛卡尔积,笛卡尔是一种错误查询方式的结果,工作切记不要出现.

  等值连接和内连接

等值连接:

select* from A,B where A.x=B.x and A.age=18;

内连接:

select* from A join B on A.x=B.x where A.age=18;(将关联关系写在on后面)

1.查询每个员工的名称和其对应的部门名称

select e.ename,d.dname from emp e join dept d on e.deptno=d.deptno;

外连接

使用外连接查询得到的数据层除了两张表的交集数据以外和另外一张主表的全部数据,哪个表为主表通过left/rigth控制, left以join左边表为主表 rigth以join右边表为主表

1.查询所有员工的名称和其对应的部门名称

select e.ename,d.dname from emp e left join dept d on e.deptno=d.deptno;

mysql 数据库求平均数问题

MySQL的AVG函数是用来求出各种记录中的字段的平均值。

MySQL中语句如下:

updateTable1setavg_price=(selectavg(price)fromTable2

whereTable2=.ID=Table1.TID)

扩展资料

在使用数据库进行数据筛选时查询时,经常会用到一些聚合函数,如count(),sum(),max(),min(),avg()

聚合函数会把NULL排除在外,但Count(*)例外,并不会排除NULL;

AVG()函数

AVG()函数返回数值列的平均值。

SQL AVG()语法

SELECT AVG(column_name) FROM table_name

sum为求平均值函数,将要求总和值的列sum(列名)

avg为求平均值函数,将要求平均值的列avg(列名)

nvl为如果未空则置空值为其他数据的函数,nvl(为空的列,将空值置成的其他值)

round为四舍五入函数,round(列名,保留小数位数)

好了,文章到这里就结束啦,如果本次分享的数据库avg和ACCESS数据库求平均值问题对您有所帮助,还望关注下本站哦!

性价比高的主机配置 什么电脑主机性价比高共享虚拟主机?共享虚拟主机和独享虚拟主机有哪些区别