oracle 10g学习之分组函数

一、

(1)分组查询语句的顺序

  select ...

  from ...

  where ...

     group by ...

  having ...

  order by ...

注意:
    where-->group by分组-->执行组函数-->having筛选->order by
    
    如果select/having语句后面出现了组函数 那么select/having后面没有被组函数修饰的列,就必须出现在group by 后面。
    组函数出现的位置 : select后面 having后面 order by后面
    SELECT列表中未出现在组函数中的所有列都必须包含在GROUP BY子句中。oracle规定where子句不可以使用分组函数,这时必须使用having子句方可完成功能。

    where筛选行
    having筛选分组
    
    不能在GROUP BY子句中使用列别名。

(2)avg:求平均值    
  count:计算记录总数            
  Max:求最大值
  Min:求最小值    
  sum:求和
  STDDEV:标准差
  VARIANCE:方差

例:查询所有员工的平均工资

  select avg(salary)

  from s_emp;

例:查询每个部门的平均工资

  select avg(salary)

  from s_emp

  group by dept_id;

例:查询部门名称及其部门的平均工资

  select dept_id,name,avg(salary)

  from s_emp,s_dept

  where s_emp.dept_id = s_dept.id

  group by dept_id,name

  order by dept_id;

例:查询平均工资大于1400的部门id,并显示出部门的名字

  select dept_id,name,avg(salary)

  from s_emp, s_dept

  where s_emp.dept_id = s_dept.id

  group by dept_id,name

  having avg(salary)>1400

例:查询title中不包含vp字符串的每个职位的平均薪水 ,并对平均薪水进行降序排列,并且每个职位的总薪水大于5000

  select title,avg(salary),sum(salary)

  from s_emp

  where lower(title) not like '%vp%'

  group bu title

  having sum(salary)>5000

  order by avg(salary) desc;

例:查询员工表中最大工资数,并显示出这个人的名字

  法一:

  select last_name,salary

  from s_emp

  where salary = (

    select max(salary)

    from s_emp

  );

  法二:

  select s1.last_name,max(s2.salary)

  from s_emp s1,s_emp s2

  group by s1.last_name

  having s1.salary=max(s2.salary);

二、子查询

子查询:一条sql语句嵌套一个或多个sql语句

例:查询工资比41号部门平均工资高的员工信息

  select last_name,salary

  from s_emp

  where salary>(

  select avg(salary)

  from s_emp

  where dept_id = 41;)

例:查询工资比Ngao所在部门平均工资高的员工信息

  select last_name.salary

  from s_emp

  where salary>(

    select avg(salary)

    from s_emp

    where dept_id =(

      select dept_id

      from s_emp

      where last_name='Ngao')

    );

例:查看部门平均工资大于32号部门平均工资的部门id

  select dept_id,avg(salary)

  from s_emp

  group by dept_id

  having avg(salary)>(

    select avg(salary)

    from s_emp

    where dept_id=32

  )

三、运行时可变参

执行sql语句的时候再传入参数

参数名字随便起,但要以&开头

        set verify on  开启校验
        set verify off  关闭校验

例:  select id,last_name,salary,dept_id
        from s_emp
        where dept_id=&dept_id;

开启校验后,会显示:

    输入 dept_id 的值:  10
           原值    3: where dept_id = &dept_id
           新值    3: where dept_id = 10

关闭校验后,则只显示:

    输入 dept_id 的值:  10

注意:输入可变参数是字符串的时候

  select id,last_name,salary

  from s_emp

  where last_name='&last_name';      输入Ngao

  select id,last_name,salary

  from s_emp

  where last_name=&last_name;        输入'Ngao'

同时设置多个可变参数:

  select &col1,&col2,salary

  from s_emp

  where &condition;

accept:定义一个变量,让用户输入值

  accept sal prompt 'sal:'

在sql语句中使用这个变量

  select id,last_name,salary

  from s_emp

  where salary>&sal;

清除变量

  undefine sal;

 

转载于:https://www.cnblogs.com/sq-software/p/5112637.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/543755.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

0.1uf与47uf并联_UF是什么形式?

0.1uf与47uf并联UF:超滤 (UF: Ultrafiltration) UF is an abbreviation of Ultrafiltration. It is a kind of membrane filtration which is used in UF water purifiers. Through a hollow fiber threaded semi-permeable membrane, the water is made to proceed…

机器学习相关——协同过滤

在现今的推荐技术和算法中,最被大家广泛认可和采用的就是基于协同过滤的推荐方法。本文将带你深入了解协同过滤的秘密。下面直接进入正题 1 什么是协同过滤 协同过滤是利用集体智慧的一个典型方法。要理解什么是协同过滤 (Collaborative Filtering, 简称 CF)&#x…

二维的完整形式是什么?

2D:二维 (2D: Two Dimensional) 2D is an abbreviation of "Two-Dimensional". 2D是“二维”的缩写 。 It is the dimension of any virtual object that has no manifestation of profundity. For example, if a graphic or picture representation of …

InfoQ中文站2015年度优秀社区编辑评选揭晓

\又到了年终岁末,在过去的一年里,InfoQ网站的月独立UV接近130万,月PV突破200万,每周独立访问用户接近30万,网站访问量过万的文章超过60篇。每月活跃的数十位社区编辑为InfoQ的内容生产贡献着力量。正是这点点汇聚的星光…

wfm扩展_WFM的完整形式是什么?

wfm扩展WFM:为我工作 (WFM: Works For Me) WFM is an abbreviation of "Works For Me". WFM是“ Works For Me”的缩写 。 It is an expression, which is commonly used in messaging or chatting on social media networking sites like Facebook, Yah…

【设计模式】—— 访问者模式Visitor

对于某个对象或者一组对象,不同的访问者,产生的结果不同,执行操作也不同。此时,就是访问者模式的典型应用了。 应用场景 1 不同的子类,依赖于不同的其他对象 2 需要对一组对象,进行许多不相关的操作&#x…

ruby宝石区块链最新消息_Ruby宝石| Ruby工具

ruby宝石区块链最新消息Ruby宝石 (Ruby Gems) Every language has its package manager which helps it by providing libraries and a standard format to distribute Ruby program. It is a type of tool which is developed to easily facilitate the installation of Gems.…

CMake入门(二)

CMake入门(二) 最后更新日期:2014-04-25 by kagula 阅读前提:《CMake入门(一)》、Linux的基本操作 环境: Windows 8.1 64bit英文版。Visual Studio 203 Update1英文版。CMake 2.8.12.2、 Cent OS 6.5。内容…

Scala中的评论

Scala评论 (Scala comments) Comments are things that are readable by the programmer. They are added to the code to add an explanation about the source code. Commenting on a program to make it easier to understand by the programmer. 注释是程序员可以读取的东西…

最小生成树prim (c++ 已大改)

2019独角兽企业重金招聘Python工程师标准>>> #include <iostream> #include <vector> #include <set> #include <map> #include <initializer_list> #include <memory> template<typename T> class Graph{private:std::m…

assoc_Ruby assoc()函数

assocRuby中的assoc()函数 (assoc() function in Ruby) We have studied functions to process single dimensional array so far but if we talk about assoc() function, it does not work for single dimensional arrays. assoc() function only works on Array of Arrays o…

前端接入HTTP协议浅析

【摘要】&#xff1a;本文整理并简要分析了HTTP协议的交互过程和内容格式&#xff0c;包括HTTP请求、HTTP应答的头域和实体内容&#xff0c;HTTP 1.0与HTTP 1.1的差异&#xff0c;并举例说明了Chunked编码的工作过程原理。1、HTTP协议简介浏览器和Web服务器之间一问一答的交互过…

互联网传真 传真指令_传真的完整形式是什么?

互联网传真 传真指令传真&#xff1a;传真 (FAX: Facsimile) FAX is an abbreviation of "Facsimile". 传真是“传真”的缩写 。 It is commonly written and spoken as FAX. It is a telephonic transmission of a scanned copy of text and images printed on a p…

C#使用七牛云存储上传下载文件、自定义回调

项目需要将音视频文件上传服务器&#xff0c;考虑并发要求高&#xff0c;通过七牛来实现。 做了一个简易的压力测试&#xff0c;同时上传多个文件&#xff0c;七牛自己应该有队列处理并发请求&#xff0c;我无论同时提交多少个文件&#xff0c;七牛是批量一个个排队处理了。 一…

netfilter que_QUE的完整形式是什么?

netfilter que问题&#xff1a;问题 (QUE: Questions) QUE is an abbreviation of "Questions". QUE是“ Questions”的缩写 。 It is an expression, which is commonly used in the Gmail platform. It is written in the body or the subject of the email to te…

mysqld_multi stop 不能停掉mysql

使用mysqld_multi start 启动了多个mysql&#xff0c;但是mysqld_multi stop 却不能停止原因&#xff1a;因为你还没有授权/usr/local/mysql/bin/mysqld_multi stop 但是默认是停不掉的&#xff0c;需要我们做一个授权grant shutdown on *.* to usernamelocalhost identified…

热冗余冷冗余_冗余支架

热冗余冷冗余Problem statement: 问题陈述&#xff1a; Given a string of balanced expression, find if it contains a redundant parenthesis or not. A set of parentheses is redundant if the same sub-expression is surrounded by unnecessary or multiple brackets. …

对称树

Problem statement: 问题陈述&#xff1a; Given a binary Tree, check whether the tree is symmetric or not. 给定二叉树 &#xff0c; 检查树是否对称 。 Input Example: 输入示例&#xff1a; For example1/ \2 2/ \ / \3 4 4 3The above tree is symmetric1/ \2 …

unity, undo

如果在操作一个Object之前调用Undo.RecordObject(Object)&#xff0c;且操作确实造成Object某些属性的改变&#xff0c;则会产生一个undo记录。 如果我们的架构不是直接操作Object&#xff0c;而是操作一个ui&#xff0c;并在某些时机通过ui.save(Object)将数据回写到Object&am…

ajax应用_AJAX的应用

ajax应用AJAX has several benefits that can be utilized inside a web application. In this article, well explore some advantages of AJAX and see some of its applications. AJAX具有可在Web应用程序内部使用的多个优点。 在本文中&#xff0c;我们将探讨AJAX的一些优势…