公司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. ubuntu下使用脚本交叉编译windows下使用的ffmpeg + X264

    这里主要是补充一些遇到的问题和解决方法. 2013-06 下旬 由于项目需要,重新编译ffmpeg+264+其他. 这里使用的环境Ubuntu 13.04,脚本依然是cross_compile_ffm ...

  2. 冒泡排序和快速排序的java实现

    转发请注明原创地址 http://www.cnblogs.com/dongxiao-yang/p/6264831.html 冒泡 public static int[] bubble_sort(int ...

  3. 非常实用的Android Studio快捷键

    One—打印Log 生成tag: logt 打印log: logm logd loge Two—代码提示 Ctrl + Alt + Space Three—代码移动 选中代码: Ctrl + w 向 ...

  4. 树莓派通过 HDMI - VGA 转接后分辨率始终为640*480无法修改的问题

    一开始装的Raspbian,感觉系统不错,就是分辨率调不了,网上找了很多解决方法,捣鼓了差不多一天,仍然没有解决. 期间尝试换了好几个系统,比如说 raspbmc .XBian等,最后试了下Pidor ...

  5. cocos2d-x触屏事件(单点触屏)

    转自:http://blog.csdn.net/onerain88/article/details/7550009 一般经常用到的触屏的情况有两种:一种是Layer统一接收触屏消息,然后由程序根据需要 ...

  6. 电话qie听器

    业务逻辑: 当有电话打进来或电话打出去的时候,对电话进行录音. public class TelphoneyListenerService extends Service { private stat ...

  7. _blank开新窗体不符合标准?

    我们要在新窗体中打开链接通常的做法是在链接后面加target="_blank",我们採用过渡型的DOCTYPE(xh tml1-transitional. dtd)时没有问题,可是 ...

  8. WSGI是一种编程接口,而uwsgi是一种传输协议

    http://uwsgi-docs.readthedocs.io/en/latest/Nginx.html http://sunxiunan.com/?p=1778 cgi---------fastc ...

  9. Spring + JDK Timer Scheduler Example--reference

    http://www.mkyong.com/spring/spring-jdk-timer-scheduler-example/ In this example, you will use Sprin ...

  10. [转] React Native Navigator — Navigating Like A Pro in React Native

    There is a lot you can do with the React Native Navigator. Here, I will try to go over a few example ...