公司TM蛋疼,动不动让你学习新东西,就是不让你闲下来,本着胳膊拧不过大腿定律,忍了,这是背景。

好吧哥端起一本厚厚的《GreenPlum企业应用实战》,打开百度开始GP的学习之路:

GP只能安装到linux系统上,本人没机会安装直接,大牛直接给的是虚拟机,上面已经配置好了环境,这里linux系统用的红帽子(redhat)。

/**gp中的基本sql语法**/
--删除表
drop table testtb;
--创建表
CREATE TABLE testtb
(
id integer,
"name" character varying(2)
)
WITH (
OIDS=FALSE
)
DISTRIBUTED BY (id);
ALTER TABLE testtb OWNER TO gpadmin;
--WITH 用来说明表的存储属性,比如表的压缩
--DISTRIBUTED BY 定义表的分布键,这个键最好唯一,如果表中没有唯一键字段,可以定义DISTRIBUTED RANDOMLY作为分布建。 --表的创建
create table testTBbak (like testTB) -- like 复制表结构
create table testtbbak1 as select * from testtb distributed by (name) -- 数据保留,可以指定分布键。
select * into testtbbak2 from testTB -- into 保留数据,不能指定分布键
select * from testtbbak2 /**分区表**/
-- 按照时间分区 -- --创建视图
create view v_testtb as select * from testtb -- 删除数据
delete from testtb -- 速度慢,会写日志。
truncate testtb -- 速度快,但不会写日期。 /**序列的使用**/
--删除序列
drop sequence myseq
--创建序列(1是开始值,可以根据实际情况改变)
create sequence myseq start 1
--序列的使用
insert into testTB values(nextval('myseq'),'小明')
--重新设置序列值
select setval('myseq',1)
--查询
select id,name from testTB order by 1 -- explain 用于查看sql执行计划
explain select * from testtb /**字符串函数**/
-- || 字符串连接
select '胡' || '黄' || '腾'
-- length 字符长度
select length('')
-- substring 字符串截取,从那位开始,截几位
select substring('' from 2 for 3)
-- trim去字符前后空格
select trim(' 234 fds ')
-- lowere 字符转小写
select lower('RER')
-- upper 字符转大写
select upper('rer')
-- replace 替换字符
select replace('aabbddee','aa','mm')
-- position 查找指定字符位置
select position('de' in 'abdeaf')
-- split_part 根据指定字符查找给定字符的位置
select split_part('aaa|bbb|ccc|eee','|',2) /**时间的函数**/
-- age求俩日期之差
select age(timestamp '1987-12-03',timestamp '2015-06-17')
-- age如果一个日期则和当前日期求差
select age(timestamp '1987-12-03')
-- current_date 当前日期
select current_date
-- current_time 当前时间
select current_time
-- current_timestamp 当前时间戳
select current_timestamp
-- now 当前时间
select now()
-- extract 获取指定时间的具体某个参数
select extract(day from date '2015-06-18')
select extract(day from now())
-- 时间相加
select '2015-04-2 10:00:52'::timestamp + interval '10 days 2 hours 10 seconds'
-- 时间相减
select current_date - interval '10 days' /**gp中其他函数**/
-- greatest 取两值中的最大值
select greatest(3,9)
-- 列转行
insert into testtb(id,name)values (1,'ha');
insert into testtb(id,name)values (1,'he');
insert into testtb(id,name)values (1,'hi');
insert into testtb(id,name)values (2,'xb');
insert into testtb(id,name)values (2,'xh');
insert into testtb(id,name)values (2,'xm');
select id,string_agg(name,'|' order by name) from testtb group by id;
-- 行转列
select id,regexp_split_to_table(string_agg,E'\\|') str from texttb_m /**开窗函数**/
--建表
create table empsalary
(
depname varchar(20),
empno integer,
salary integer
)
distributed by (empno) insert into empsalary values ('develop',9,4500);
insert into empsalary values ('develop',1,3200);
insert into empsalary values ('develop',4,1000);
insert into empsalary values ('develop',2,9100);
insert into empsalary values ('develop',6,1000);
insert into empsalary values ('person1',5,3100);
insert into empsalary values ('person1',7,4100);
insert into empsalary values ('sales',3,2400);
insert into empsalary values ('sales',8,1200);
insert into empsalary values ('sales',10,5100); -- rank 及 row_number 函数的应用
select depname
,empno
,salary
,rank() over (partition by depname order by salary desc)
,row_number() over (partition by depname order by salary desc)
from empsalary
-- rank 识别重复记录
-- row_number 不识别重复记录 select *
,sum(salary) over () sum1
,sum(salary) over (order by salary) sum2
,sum(salary) over (partition by depname) sum3
,sum(salary) over (partition by depname order by salary) sum4
from empsalary -- grouping by 的使用(其实就是简化了union)
select depname ,sum(empno)
from empsalary
group by depname
union all
select depname ,sum(salary)
from empsalary
group by depname --等效于
select depname,sum(empno),sum(salary)
from empsalary
group by grouping sets(depname)

greenplum学习的更多相关文章

  1. GreenPlum学习之(Share-nothing)架构

    当今世界是一个信息化的世界,我们的生活中无论是生活.工作.学习都离不开信息系统的支撑.而信息系统的背后用于保存和处理最终结果的地方就是数据库.因此数据库系统就变得尤为重要,这意味着如果数据库如果面临问 ...

  2. GreenPlum学习笔记:基础知识

    一.介绍 GreenPlum分布式数据仓库,大规模并行计算技术. 无共享/MPP核心架构 Greenplum数据库软件将数据平均分布到系统的所有节点服务器上,所以节点存储每张表或表分区的部分行,所有数 ...

  3. GreenPlum学习笔记:create table创建表

    二维表同样是GP中重要的存储数据对象,为了更好的支持数据仓库海量数据的访问,GP的表可以分成: 面向行存储的普通堆积表 面向列存储的AOT表(append only table) 当然AOT表也可以是 ...

  4. GreenPlum学习笔记:create or replace function创建函数

    原始表数据如下: 需求:现要求按分号“;”将rate_item列进行分割后插入到新的数据表中. CREATE OR REPLACE FUNCTION fun_gp_test_xxx_20181026( ...

  5. GreenPlum学习笔记:新手入门命令

    1.命令行登录数据库 psql -h 192.168.111.111 -U username -d dbname 其中,username为数据库用户名,dbname为数据库名,执行后提示输入密码.(可 ...

  6. GreenPlum学习笔记:date_part与extract提取日期时间、时间差

    GP可以使用date_part / extract从日期时间类型中抽取部分内容. 方法一:extract 格式:extract(field from source)  extract函数从日期.时间数 ...

  7. GreenPlum学习笔记:split_part与string_to_array字符截取

    偶遇一个需求:想按某个指定符号分割之后,提取字符. 例如:tag = '休闲,娱乐,运动,玩耍',想提取"休闲"这个词. 方法一:string_to_array select st ...

  8. Greenplum:学习资料

    Greenplum技术浅析:http://www.cnblogs.com/end/archive/2012/08/17/2644290.html Greenplum 数据库架构分析:http://ww ...

  9. Greenplum+Hadoop学习笔记-14-定义数据库对象之创建与管理模式

    6.3.创建与管理模式 概述:DB内组织对象的一种逻辑结构.一个DB内能够有多个模式.在未指定模式时默认放置在public中.能够通过"\dn"方式查看数据库中现有模式: test ...

随机推荐

  1. centos git版本服务器配置

    在服务器上安装git及做些操作 - 执行命令 ` sudo yum install curl-devel expat-devel gettext-devel openssl-devel zlib-de ...

  2. puppet重申证书

    直接上步骤,由于测试用的是PE3.X版本,在网上搜的命令几乎与PE相关的puppet命令不同了, 1.在PE-Client操作,停止pe-puppet,pe-mcollective资源; puppet ...

  3. poj3177--Redundant Paths(边的双连通)

    有n个牧场,Bessie 要从一个牧场到另一个牧场,要求至少要有2条独立的路可以走.现已有m条路,求至少要新建多少条路,使得任何两个牧场之间至少有两条独立的路.两条独立的路是指:没有公共边的路,但可以 ...

  4. 教程-Delphi资源文件(全面分析于使用)

    Delphi资源文件(全面分析之位图.光标.图标.AVI.JPEG.Wave)   几乎每个Windows应用程序都使用图标.图片.光标等资源.资源是程序的一部分,但是它是不可执行代码.下面我们就详细 ...

  5. A Tour of Go Slicing slices

    ---恢复内容开始--- Slices can be re-sliced, creating a new slice value that points to the same array. The ...

  6. JavaScript constructors, prototypes, and the `new` keyword

    Are you baffled(阻碍:使迷惑) by the new operator in JavaScript? Wonder what the difference between a func ...

  7. Static NAT with iptables on Linux

    本文的名字取的比较有意义,因为本文并不是真的要讨论如何在Linux上使用iptables实现static nat!之所以这么命名本文,是想引起别人的注意,因为中文资料,以及国内的搜索引擎,基本上没有人 ...

  8. UVALive 6088 Approximate Sorting 构造题

    题目链接:点击打开链接 题意: 给定一个n*n的01矩阵 我们跑一下例子== 4 0111 0000 0100 0110 0123 \|____ 0|0111 1|0000 2|0100 3|0110 ...

  9. Unicode编码及其实现:UTF-16、UTF-8,and more

    http://blog.csdn.net/thl789/article/details/7506133

  10. js渐变显示渐变消失

    以下是渐变的js代码(表示多余三行的要隐藏,点击"more"显示剩下的,点击“less”要逐渐隐藏): function showAccomplishmentTableRow(){ ...