sql聚合函数_SQL聚合函数

sql聚合函数_SQL聚合函数sql聚合函数SQLAggregateFunctionsbasicallyoperateonmultiplecolumnstoperformtheoperationsandservetorepresenttheoutputasanentityrepresentingtheoperationexecuted.SQL聚合函数基本上在多个列上进行操作以执…

大家好,又见面了,我是你们的朋友全栈君。

sql聚合函数

SQL Aggregate Functions basically operate on multiple columns to perform the operations and serve to represent the output as an entity representing the operation executed.

SQL聚合函数基本上在多个列上进行操作以执行操作,并将输出表示为表示所执行操作的实体。

SQL Aggregate Functions
SQL Aggregate Functions
SQL聚合函数

Syntax:

句法:

aggregate_function (DISTINCT | ALL expression)
  • DISTINCT enables the user to select distinct values from the table i.e. if multiple attributes contain the same value, then only single distinct value is considered for the calculations.

    DISTINCT使用户可以从表中选择不同的值,即,如果多个属性包含相同的值,则仅考虑单个不同的值进行计算。

  • ALL makes sure that even the repeated values are considered for the calculations.

    ALL确保即使重复值也被考虑用于计算。



SQL中的聚合函数: (Aggregate Functions in SQL:)

  • AVG()

    AVG()

  • MIN()

    MIN()

  • MAX()

    MAX()

  • SUM()

    和()

  • COUNT()

    计数()



1. SQL AVG()函数 (1. SQL AVG() function)

SQL AVG() function returns the average of all the selected values from the corresponding column of the table.

SQL AVG()函数从表的相应列返回所有选定值的平均值

Let’s us consider the following table to understand the Aggregate functions:

让我们考虑下表以了解Aggregate函数:

Table Name: Details

表名称: 详细信息

ID Name Amount Age
1 Safa 5000 21
2 Aman 2500 23
3 Rehaan 20000 25
4 Seema 12000 25
ID 名称 年龄
1个 萨法 5000 21
2 阿曼 2500 23
3 瑞安 20000 25
4 西玛 12000 25

Example:

例:


select AVG(Amount) from Details;

Output:

输出:

9875


2. SQL MIN()函数 (2. SQL MIN() function)

SQL MIN() function returns the minimum value of all the selected values from the corresponding column of the table.

SQL MIN()函数从表的相应列返回所有选定值的最小值

Example:

例:


select MIN(Amount) from Details;

Output:

输出:

2500


3. SQL MAX()函数 (3. SQL MAX() function)

SQL MAX() function returns the maximum value of all the selected values from the corresponding column of the table.

SQL MAX()函数从表的相应列返回所有选定值的最大值


select MAX(Amount) from Details;

Output:

输出:

20000


4. SQL SUM()函数 (4. SQL SUM() function)

SQL SUM() function returns the summation of all the selected values from the corresponding column of the table.

SQL SUM()函数从表的相应列返回所有选定值的总和

示例1: SUM()函数的基本理解 (Example 1: Basic Understanding of SUM() Function)


select SUM(Amount) from Details;

Output:

输出:

39500

示例2:带有GROUP BY子句SQL SUM() (Example 2: SQL SUM() with GROUP BY Clause)

SQL SUM() can be framed together with SQL GROUP BY Clause to represent the output results by a particular label/values.

SQL SUM()可以与SQL GROUP BY子句一起使用,以特定的标签/值表示输出结果。


SELECT SUM(Amount),Name  
FROM Details 
WHERE Age>21 
GROUP BY Name;  

Output:

输出:

SQL SUM() with GROUP BY
SUM() with GROUP BY
SUM()与GROUP BY

示例3:具有HAVING子句SQL SUM() (Example 3: SQL SUM() with HAVING Clause)

SQL SUM() function can be used along with SQL HAVING Clause; HAVING Clause is basically used to specify the condition to be operated on the set of values in the table.

SQL SUM()函数可以与SQL HAVING子句一起使用。 HAVING子句基本上用于指定要对表中的一组值进行操作的条件。


SELECT SUM(Amount),Name  
FROM Details 
GROUP BY Name
HAVING SUM(Amount)>2500;

Output:

输出:

SQL SUM() with HAVING Clause
SUM() with HAVING Clause
具有HAVING子句的SUM()



5. SQL COUNT()函数 (5. SQL COUNT() function)

示例 1: COUNT()函数返回表中特定列的计数 (Example 1: COUNT() function to return the count of a particular column of a table)


select COUNT(Amount) from Details;

Output:

输出:

4

示例2:COUNT(*)函数 (Example 2: COUNT(*) function)

This function returns the count of all the values present in the set of records of a table.

此函数返回表的记录集中存在的所有值的计数。


SELECT count(*) from Details;

Output:

输出:

4

示例3:带有WHERE子句的COUNT() (Example 3: COUNT() with WHERE Clause)


SELECT count(*) from Details
where Age<25;

Output:

输出:

2

示例4:COUNT()和GROUP BY子句 (Example 4: COUNT() with GROUP BY Clause)


SELECT count(Amount),Name from Details
where Age<25
Group by Name;

Output:

输出:

SQL COUNT() with GROUP BY Clause
COUNT() with GROUP BY Clause
COUNT()和GROUP BY子句

示例5:带有HAVING子句的COUNT() (Example 5: COUNT() with HAVING Clause)


SELECT count(Amount),Age, Name from Details
Group by Name
HAVING Age>=25;

Output:

输出:

SQL COUNT() with HAVING Clause
COUNT() with HAVING Clause
COUNT()和HAVING子句



结论 (Conclusion)

Thus, in this article, we have understood different SQL Aggregate Functions.

因此,在本文中,我们了解了不同SQL聚合函数。



参考 (Reference)

翻译自: https://www.journaldev.com/34288/sql-aggregate-functions

sql聚合函数

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/151828.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Keil MDK 2020过期问题[通俗易懂]

    Keil MDK 2020过期问题[通俗易懂]KeilMDK2020过期问题由于到2020年过期,之前曾担心到2020年是否我们用KEILMDK所编写的代码,全部不可用。经过今天测试,虽然软件提示过期,不过依然可以正常使用,只是没有软件的支持维护而已。用微信扫描二维码为博主打个赏金额随意快来“打”我呀要买枸杞当归补补~~转自:https://www.zhjm.site/wordpress/?p=340…

    2022年5月9日
    206
  • SpringBoot开发常见技术整合【学习笔记整理】

    SpringBoot开发常见技术整合【学习笔记整理】

    2021年7月12日
    97
  • 利用git上传本地文件、文件夹到Github

    利用git上传本地文件、文件夹到Github利用git上传文件至github是特别常用的,总结以下内容供参考使用。第一步:下载git工具,[这里是链接](https://git-scm.com/downloads),选择适合自己的版本进行安装。第二步:安装完成后,找到Gitbash,双击打开。第三步:输入自己的用户名和邮箱(为注册GITHUB账号时的用户名和邮箱)$gitconfig–globaluser.name”co

    2022年5月13日
    40
  • USB转RS485串口电路设计「建议收藏」

    USB转RS485串口电路设计「建议收藏」USB转串口芯片的串口信号一般为TTL/CMOS电平,在实现半双工RS485串口时需要外接485电平转换芯片,设计中需要有信号来控制485转接芯片的发送和接收使能端,建议选择自带485控制引脚的转接芯片(如CH340/CH342系列芯片的TNOW引脚),该引脚默认为低电平,当串口处于发送状态时会自动拉高处于有效状态,发送完成再恢复低电平。同理,可以延伸到其他应用场景,如单片机串口转485电路设计中可以使用GPIO口来控制485转接芯片的发送和接收使能。以MAX485为例:1.DE..

    2022年6月10日
    59
  • flex布局垂直居中

    flex布局垂直居中使用flex布局实现下面图中效果:外框高都为400px,边框为2px;圆的宽高为100px;中圆是水平居中;下圆是水平居中以及相对于中圆垂直居中(下圆到中圆的距离和下圆到下边框的距离相等)。效果如图:我的实现方法是笨办法,大佬们多指点<divclass=”box”><divclass=”item”><divclass=”child”></div></di

    2022年6月12日
    38
  • 卷王指南,大学计算机专业,面临分专业,计科,软工,大数据,物联网,网络工程,该选什么?

    卷王指南,大学计算机专业,面临分专业,计科,软工,大数据,物联网,网络工程,该选什么?同学们好,我是王老师——二哥呀!(笑喷)好巧!前几天有同学私信问过我这个问题:大学计算机专业,面临分专业,计科,软工,大数据,物联网,网络工程,该选什么?再加上高考结束后填报志愿,想必CSDN上有很多同学挺迷茫的。我就来(主观地)一一分析下,从后往前。网络工程,听起来还算是蛮高端大气上档次的,大家可以去百度百科或者维基百科看一下这个专业的解释。我的理解是网络工程是将计算机以及其他设备串联完成网络通信,以及智能化监控的学科。主要的分支有:路由交换、网络安全、无线网络等等。我之前在的一个公司(十年

    2022年6月9日
    45

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号