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);

  • 相关阅读:
    《Linux Device Drivers》第十二章 PCI司机——note
    Swift开放StatsD后上传数据的出现,出现退换货503的Bug
    google login page
    Use GraceNote SDK in iOS(一)通过序列化GDO查询专辑封面
    【人在职场】能力与价值
    HDU 5067-Harry And Dig Machine(DFS)
    LeetCode:Merge Two Sorted Lists
    HTML5硕士学习笔记
    通过设置注册表隐藏桌面图标
    SharePoint 要一个多行文本类型字段为特殊类型的链接
  • 原文地址:https://www.cnblogs.com/kongkongFabian/p/9990371.html
Copyright © 2011-2022 走看看