zoukankan      html  css  js  c++  java
  • Sql Server 聚集函数

    /*
    聚集函数

    AVG 求平均值
    SUM 求和
    COUNT 求行数 :如果是 count(*) 那么如果一行里全是NULL则也可以查到此行。 如果是count(具体列名) 此列中某一行为NULL
    MAX 求最大值
    MIN 求最小值


    ALL 全部
    DISTINCT 不同的
    */


    INSERT INTO dbo.class(ID,NAME) VALUES(1,'张三');

    INSERT INTO dbo.class(ID,NAME) VALUES(null,null);


    select count(id) from class

    select COUNT(*) from class;

    select COUNT(*) from class;

    create table abc(id int ,name varchar(10)) ;

    select * from dbo.abc;

    select * from Products


    select AVG(prod_price) from Products; --AVG 求这一列总和的平均值

    select SUM(prod_price) from products; --SUM 求这一列的总和

    select MAX(PROD_PRICE) FROM Products; -- MAX 求这一列中最大的值

    select MIN(PROD_PRICE) FROM Products; --MIN 求这一列中最小的值

    select COUNT(prod_price) from Products; --COUNT 求这一列有多少行

    select COUNT(cust_email) from Customers; --email中有NULL值, count统计多少行记录 不会统计NULL值 总行数5 三个邮箱有值,两个为NULL。 所以count统计的是3条记录。

    select COUNT(*) from Customers; --

    SELECT COUNT(*) FROM Customers WHERE cust_email<>NULL AND cust_address <>NULL AND cust_city<>NULL AND cust_contact<>NULL AND cust_country<>NULL AND cust_id<>NULL AND cust_name<>NULL AND cust_state<>NULL AND cust_zip<>NULL;

    SELECT * FROM Customers;

    select LEN(prod_price) from Products; --LEN 或者 DATALENGTH 求这一列每行的字符长度

    select prod_name from Products;
    select MAX(prod_name) from Products;

    SELECT AVG(DISTINCT PROD_PRICE) FROM Products; --对不同的记录 算出平均值。 这两个语句得出的结果时不同的。

    select AVG( all prod_price ) from Products; --对全部行记录 算出平均值。

    SELECT COUNT(DISTINCT prod_price) FROM Products; -- DISTINCT distinct(不同的 distinct)

    SELECT COUNT(ALL PROD_PRICE) FROM Products; --对行进行计算时 默认使用的是ALL(全部) 就是不指定ALL也是默认为ALL。 如果想要得出不同的记录(不重复的记录) 则指定distinct;



    SELECT * FROM Products;

  • 相关阅读:
    struts2解决动态多文件上传的问题(上传文件与数据库字段一一对应)(转)
    Android应用如何开机自启动、自启动失败原因
    android操作ini工具类
    C++#define的用法(含特殊)
    c指针
    WP7备注(34)(UpdateSourceTrigger|ValidatesOnExceptions|NotifyOnValidationError)
    WP7备注(30)(数据绑定基本)
    WP7备注(38)(VisualStateManager)
    WP7备注(39)(ResourceDictionary)
    WP7备注(35)(Vector Graphics|Raster Graphics)
  • 原文地址:https://www.cnblogs.com/java-263/p/13547158.html
Copyright © 2011-2022 走看看