日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

group?by用法詳解_oracle

作者:IT界一股清流 ? 更新時間: 2022-02-13 編程語言

一. ?概述

group_by的意思是根據by對數據按照哪個字段進行分組,或者是哪幾個字段進行分組。

二. ?語法

select ? 字段 ? ?from ? 表名 ? where ? ?條件 ? ? group ? by? ? ? ?字段

或者

select ? 字段 ? ?from ? 表名 ? group ?by ? ?字段 ? ?having ? ?過濾條件

注意:對于過濾條件,可以先用where,再用group ?by或者是先用group ?by,再用having

三. ?案例

1 ?創建表格并插入數據

說明:在plsql ?developer上創建表格并插入數據,以便下面進行簡單字段分組以及多個字段分組,同時還結合聚合函數進行運算。

創建student表

? ? ? create table student

(id ?int not null ,

name varchar2(30),

grade varchar2(30),

salary ?varchar2(30)

)

在student表中插入數據

insert into student values(1,'zhangsan','A',1500);

insert into student values(2,'lisi','B',3000);

insert into student values(1,'zhangsan','A',1500);

insert into student values(4,'qianwu','A',3500);

insert into student values(3,'zhaoliu','C',2000);

insert into student values(1,'huyifei','D',2500);

數據插入到student表中的結果

2 ?單個字段分組

① ?select ? grade ? from ? student ? ? ? ? ? ? ? 查出所有學生等級(包括重復的等級)

② ?select ?grade ?from ?student ? group ? by ? grade ? ? ? 查出學生等級的種類(按照等級劃分,去除重復的)

3 ?多個字段分組

select ?name , sum(salary) ? ?from ? student ? ?group ?by ? name , grade ? ? ?按照名字和等級劃分,查看相同名字下的工資總和

注意:這里有一點需要說明一下,多個字段進行分組時,需要將name和grade看成一個整體,只要是name和grade相同的可以分成一組;如果只是name相同,grade不同就不是一組。

4 ?配合聚合函數一起使用

常用的聚合函數:count() , sum() , avg() , max() , min()

count():計數

select ?name , count(*) ?from ?student ? group ?by ?name ? ? ? ? ? 查看表中相同人名的個數

得出的如下結果

sum():求和

select ?name , sum(salary) ? from ? student ? group ?by ? name ? ? ?查看表中人員的工資和(同姓的工資相加)

得出的如下結果

avg():平均數

select ?name , avg(salary) ? from ?student ? group ?by ?name ?, grade ? ? ??查看表中人員的工資平均數(同姓工資平均數)

得出的如下結果

max():最大值

select ? grade , max(salary) ? from ? student ? group ?by ? grade ? ? ? ? ??查看按等級劃分人員工資最大值

得出的如下結果

min():最小值

select ? grade , min(salary) ? from ? student ? group ?by ? grade ? ? ? ?查看按等級劃分人員工資最小值

得出的如下結果

原文鏈接:https://blog.csdn.net/jerrytomcat/article/details/82351605

欄目分類
最近更新