加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

postgreSQL高阶功能_02

发布时间:2020-12-13 16:10:43 所属栏目:百科 来源:网络整理
导读:-- 测试表 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 (emplo

-- 测试表
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,
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‘),‘2002-06-01‘),‘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‘,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,sum(quantity) from sales
group by grouping sets ((brand,segment),(brand),(segment),());

select grouping(brand) as grouping_brand,grouping(segment) as grouping_segment,brand,()) order by brand,segment;

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

select grouping(brand) as grouping_brand,segment) order by brand,segment;


-- rollup 在其上进行快捷的操作
select brand,sum(quantity) from sales
group by rollup (brand,segment);
-- 下面sql查询的分组集有
-- (segment,brand)
-- (segment)
-- ()
select brand,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.98select film_id,rental_rate from film where rental_rate > (select avg(rental_rate) from film);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读