zoukankan      html  css  js  c++  java
  • postgreSQL高阶功能_02

    -- 测试表
    CREATE TABLE employees (
    employee_id serial PRIMARY KEY,
    employee_name VARCHAR (255) NOT NULL
    );

    CREATE TABLE keys (
    employee_id INT PRIMARY KEY,
    effective_date DATE NOT NULL,
    FOREIGN KEY (employee_id) REFERENCES employees (employee_id)
    );

    CREATE TABLE hipos (
    employee_id INT PRIMARY KEY,
    effective_date DATE NOT NULL,
    FOREIGN KEY (employee_id) REFERENCES employees (employee_id)
    );

    -- 测试数据
    INSERT INTO employees (employee_name)
    VALUES
    ('Joyce Edwards'),
    ('Diane Collins'),
    ('Alice Stewart'),
    ('Julie Sanchez'),
    ('Heather Morris'),
    ('Teresa Rogers'),
    ('Doris Reed'),
    ('Gloria Cook'),
    ('Evelyn Morgan'),
    ('Jean Bell');

    INSERT INTO keys
    VALUES
    (1, '2000-02-01'),
    (2, '2001-06-01'),
    (5, '2002-01-01'),
    (7, '2005-06-01');

    INSERT INTO hipos
    VALUES
    (9, '2000-01-01'),
    (2, '2002-06-01'),
    (5, '2006-06-01'),
    (10, '2005-06-01');

    select employee_id from keys

    select employee_id from hipos

    select employee_id from keys
    -- 返回交集,同时存在两个表查询出来的数据
    intersect
    select employee_id from hipos
    order by employee_id desc

    select employee_id from keys
    -- 返回并集
    union
    select employee_id from hipos

    select employee_id from keys
    -- 返回差集,
    except
    select employee_id from hipos


    ---------------- ---------------------
    CREATE TABLE sales (
    brand VARCHAR NOT NULL,
    segment VARCHAR NOT NULL,
    quantity INT NOT NULL,
    PRIMARY KEY (brand, segment)
    );

    INSERT INTO sales (brand, segment, quantity)
    VALUES
    ('ABC', 'Premium', 100),
    ('ABC', 'Basic', 200),
    ('XYZ', 'Premium', 100),
    ('XYZ', 'Basic', 300);

    select * from sales;
    select brand,sum(quantity) from sales group by brand;
    select segment,sum(quantity) from sales group by segment;
    select sum(quantity) from sales;

    -- 分组集 grouping sets
    -- 这个有点不实用,但留个印象,存在即合理
    select brand,segment,sum(quantity) from sales
    group by grouping sets ((brand,segment),(brand),(segment),());

    select grouping(brand) as grouping_brand,grouping(segment) as grouping_segment,brand,segment,sum(quantity) from sales
    group by grouping sets ((brand,segment),(brand),(segment),()) order by brand,segment;

    -- cube 在 grouping sets 的基础上进行的简化操作,会生成所有可能的分组集
    select brand,segment,sum(quantity) from sales
    group by cube (brand,segment);

    select grouping(brand) as grouping_brand,grouping(segment) as grouping_segment,brand,segment,sum(quantity) from sales
    group by cube (brand,segment) order by brand,segment;


    -- rollup 在其上进行快捷的操作
    select brand,segment,sum(quantity) from sales
    group by rollup (brand,segment);
    -- 下面sql查询的分组集有
    -- (segment,brand)
    -- (segment)
    -- ()
    select brand,segment,sum(quantity) from sales
    group by rollup (segment,brand);

    -- EXTRACT() 函数用于返回日期/时间的单独部分
    select * from rental;
    select extract(year from rental_date) as y,count(rental_id) from rental group by rollup(extract(year from rental_date))

    ___________________________________________________
    -- 子查询
    select AVG(rental_rate) from film;
    select film_id,title,rental_rate from film where rental_rate > 2.98
    select film_id,title,rental_rate from film where rental_rate > (select avg(rental_rate) from film);

  • 相关阅读:
    困扰我的c++语法
    C++ primer第三章作业
    渔夫捕鱼问题
    JAVA输入输出
    Python学习5——抽象,涉及抽象和结构、函数的自定义、参数、作用域、递归
    牛顿迭代法计算平方根
    Python学习4——条件、循环及其他语句总结
    Python补充1——Python的简单推导
    Python补充2——Python单行注释、整段注释使用方法
    Python补充3——Python中的 split() 函数
  • 原文地址:https://www.cnblogs.com/kongkongFabian/p/9990371.html
Copyright © 2011-2022 走看看