7.5.1. generate_series

生成序列数

test=# select generate_series(1,10) as id;id
----12345678910
(10 rows)

7.5.2. 日期/时间

7.5.2.1. Date/Time Operators

日期时间的偏移计算

select now() + interval '3 day';
select now() - interval '3 day';select now() + interval '1 hour';
select now() - interval '1 hour';select now() + interval '10 minutes';
select now() - interval '10 minutes';select now() + interval '5 days 3 hours';
select now() + interval '5 days' + interval '3 hours';

7.5.2.2. 当前日期/时间

3.1.1   当前日期current_date
netkiller=> select current_date;date
------------2003-11-28
(1 row)netkiller=>3.1.2   当前时间current_time
netkiller=> select current_time;timetz
--------------------19:38:47.270235+08
(1 row)netkiller=>3.1.3   当前日期时间current_timestamp
netkiller=> select current_timestamp;timestamptz
-------------------------------2003-11-28 19:39:25.548505+08
(1 row)netkiller=>
3.1.4   除去时区1.         current_timestamp::timestamp (0)
2.         current_timestamp::timestamp (0) without time zone;
netkiller=> select current_timestamp::timestamp (0);timestamp
---------------------2003-11-28 19:44:33
(1 row)netkiller=>
netkiller=> select current_timestamp::timestamp (0) without time zone;timestamp
---------------------2003-11-28 19:40:10
(1 row)

now() / clock_timestamp() 函数

select now();
SELECT clock_timestamp();           

7.5.2.3. 时间计算

3.1.5   计算时间差netkiller=> select to_date('2003-12-2','YYYY-MM-DD')-to_date('2003-12-1','YYYY-MM-DD');
?column?
----------
1
(1 row)netkiller=>
netkiller=> select to_date('2003-12-2','YYYY-MM-DD')-to_date('2003-10-2','YYYY-MM-DD');
?column?
----------
61
(1 row)
3.1.6   计算时间和netkiller=> select to_date('2003-12-6','yyyy-mm-dd')+12 ;
?column?
------------
2003-12-18
(1 row)netkiller=> select to_date('2003-12-6','yyyy-mm-dd')+20 ;
?column?
------------
2003-12-26
(1 row)

7.5.2.4. to_char() / to_date()

to_char()

select count(*) as c, to_char(ctime, 'yyyy-mm') as dt from practice group by dt order by dt;select count(*) as c, to_char(ctime, 'yyyy-mm-dd') as dt from practice group by dt order by dt;select count(*) as c, to_char(ctime, 'yyyy-mm-dd hh') as dt from practice group by dt order by dt;

7.5.2.5. EXTRACT, date_part

select extract (year from now());
select extract (month from now());
select extract (day from now());
select extract (day from timestamp '2013-06-03');
select extract (hour from now());
select extract (minute from now());
select extract (second from now());
select extract (week from now());SELECT extract(century FROM now());
3.1.7   date_partnetkiller=> select date_part('epoch', '2003-12-3 10:20:30' - timestamp '2003-12-1 02:00:00') ;date_part
-----------202830
(1 row)netkiller=> select date_part('day', '2003-12-3 10:20:30' - timestamp '2003-12-1 02:00:00') ;date_part
-----------2
(1 row)netkiller=> select date_part('hour', '2003-12-3 10:20:30' - timestamp '2003-12-1 02:00:00') ;
date_part
-----------8
(1 row)netkiller=>

7.5.2.6. date_trunc

select count(*) as c, date_trunc('day', ctime) as dt from practice group by dt;

7.5.2.7. 延迟执行

pg_sleep(seconds)

SELECT pg_sleep(1.5);

7.5.2.8. 时区

SELECT now() AT TIME ZONE 'GMT';
SELECT now() AT TIME ZONE 'GMT+8';           

7.5.3. uuid

create extension "uuid-ossp";
create table products (product_id  uuid primary key default uuid_generate_v4());

7.5.4. tablefunc

http://www.postgresql.org/docs/9.1/static/tablefunc.html

确认扩展是否已经安装

$ ls -1 /usr/pgsql-9.3/share/extension/tablefunc*
/usr/pgsql-9.3/share/extension/tablefunc--1.0.sql
/usr/pgsql-9.3/share/extension/tablefunc.control
/usr/pgsql-9.3/share/extension/tablefunc--unpackaged--1.0.sql       

安装扩展

$ psql test
psql (9.3.1)
Type "help" for help.test=# create extension tablefunc;
CREATE EXTENSION
test=# \q      

数据库结构

-- Table: account-- DROP TABLE account;CREATE TABLE account
(id SERIAL NOT NULL,name character varying(10) NOT NULL, -- 账号balance money NOT NULL DEFAULT 0.00, -- 余额datetime timestamp without time zone NOT NULL DEFAULT (now())::timestamp(0) without time zone,CONSTRAINT account_pkey PRIMARY KEY (id)
)
WITH (OIDS=FALSE
);
ALTER TABLE accountOWNER TO dba;
COMMENT ON COLUMN account.name IS '账号';
COMMENT ON COLUMN account.balance IS '余额';-- Index: account_name_idx-- DROP INDEX account_name_idx;CREATE INDEX account_name_idxON accountUSING btree(name COLLATE pg_catalog."default");       

测试数据

INSERT INTO account (id, name, balance, datetime) VALUES (1, 'Neo', '$0.00', '2013-10-09 10:51:10');
INSERT INTO account (id, name, balance, datetime) VALUES (2, 'Neo', '$12.60', '2013-10-09 10:51:22');
INSERT INTO account (id, name, balance, datetime) VALUES (3, 'Neo', '$200.00', '2013-10-09 10:11:52');
INSERT INTO account (id, name, balance, datetime) VALUES (4, 'Neo', '-$6.80', '2013-10-09 10:51:42');
INSERT INTO account (id, name, balance, datetime) VALUES (5, 'Neo', '$100.00', '2013-10-09 10:52:49');
INSERT INTO account (id, name, balance, datetime) VALUES (6, 'Jerry', '$200.00', '2013-10-09 10:56:35');
INSERT INTO account (id, name, balance, datetime) VALUES (7, 'Jerry', '$50.45', '2013-10-09 10:57:23');
INSERT INTO account (id, name, balance, datetime) VALUES (8, 'Jerry', '$75.50', '2013-10-09 10:57:31');
INSERT INTO account (id, name, balance, datetime) VALUES (9, 'Jerry', '-$55.30', '2013-10-09 10:59:28');
INSERT INTO account (id, name, balance, datetime) VALUES (10, 'Jerry', '-$200.00', '2013-10-09 10:59:44');
INSERT INTO account (id, name, balance, datetime) VALUES (11, 'Tom', '$5.00', '2013-10-15 13:26:19');
INSERT INTO account (id, name, balance, datetime) VALUES (12, 'Neo', '$50.60', '2013-10-15 13:26:34');
INSERT INTO account (id, name, balance, datetime) VALUES (13, 'Jerry', '$62.08', '2013-10-15 13:26:51');
INSERT INTO account (id, name, balance, datetime) VALUES (14, 'Tom', '$70.00', '2013-10-15 13:27:01');
INSERT INTO account (id, name, balance, datetime) VALUES (15, 'Neo', '-$20.56', '2013-10-15 13:27:19');
INSERT INTO account (id, name, balance, datetime) VALUES (16, 'Tom', '$70.00', '2013-10-16 13:27:01');
INSERT INTO account (id, name, balance, datetime) VALUES (17, 'Jerry', '$70.00', '2013-10-16 13:27:01');
INSERT INTO account (id, name, balance, datetime) VALUES (18, 'Jerry', '-$30.00', '2013-10-16 13:30:01');
INSERT INTO account (id, name, balance, datetime) VALUES (19, 'Neo', '-$30.00', '2013-10-16 13:35:01');
INSERT INTO account (id, name, balance, datetime) VALUES (20, 'Tom', '-$30.00', '2013-10-16 13:35:01');

查询语句

SELECT * FROM crosstab('select datetime,name,balance from account order by 1,2','select name from account group by name order by 1')  AS account(datetime timestamp, Jerry character varying, Neo character varying, Tom character varying);       

终端输出

      datetime       |  jerry   |   neo   |   tom
---------------------+----------+---------+---------2013-10-09 10:11:52 |          | $200.00 |2013-10-09 10:51:10 |          | $0.00   |2013-10-09 10:51:22 |          | $12.60  |2013-10-09 10:51:42 |          | -$6.80  |2013-10-09 10:52:49 |          | $100.00 |2013-10-09 10:56:35 | $200.00  |         |2013-10-09 10:57:23 | $50.45   |         |2013-10-09 10:57:31 | $75.50   |         |2013-10-09 10:59:28 | -$55.30  |         |2013-10-09 10:59:44 | -$200.00 |         |2013-10-15 13:26:19 |          |         | $5.002013-10-15 13:26:34 |          | $50.60  |2013-10-15 13:26:51 | $62.08   |         |2013-10-15 13:27:01 |          |         | $70.002013-10-15 13:27:19 |          | -$20.56 |2013-10-16 13:27:01 | $70.00   |         | $70.002013-10-16 13:30:01 | -$30.00  |         |2013-10-16 13:35:01 |          | -$30.00 | -$30.00
(18 rows)

原文出处:Netkiller 系列 手札
本文作者:陈景峯
转载请与作者联系,同时请务必标明文章原始出处和作者信息及本声明。

7.5. Function相关推荐

  1. 创建一个Scalar-valued Function函数来实现LastIndexOf

    昨天有帮助网友解决的个字符串截取的问题,<截取字符串中最后一个中文词语(MS SQL)>http://www.cnblogs.com/insus/p/7883606.html 虽然实现了, ...

  2. php function集合

    /*更新商品的某个字段*/ function update_goods($goods_id, $field, $value) {if ($goods_id){/* 清除缓存 */clear_cache ...

  3. 函数指针amp;绑定: boost::functoin/std::function/bind

    see link: https://isocpp.org/wiki/faq/pointers-to-members function vs template: http://stackoverflow ...

  4. class function或class procedure是什么意思

    类函数\类过程.   它们是直接操作在类上面(没有实例化的对象) 下面是Delphi    Help    的描述            A class method is a method (oth ...

  5. Cost Function

    首先本人一直有一个疑问缠绕了我很久,就是吴恩达老师所讲的机器学习课程里边的逻辑回归这点,使用的是交叉熵损失函数,但是在进行求导推导时,google了很多的课件以及教程都是直接使用的,这个问题困扰了很久 ...

  6. error C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. 的解决方法

    vs2013编译出错信息:错误    1    error C4996: 'fopen': This function or variable may be unsafe. Consider usin ...

  7. c++11 function

    是代指返回值为void型的函数指针. function 最大的功能是表达匿名函数,特别是 [] 里面捕捉了当前上下文变量的匿名函数 类模版 std::function是一种通用.多态的函数封装.std ...

  8. 避开移入移出事件内部div干扰事件,e是function(e)的e

    // 避开移入移出事件内部div干扰事件 // e是function(e)的elet element = e.toElement || e.relatedTarget;if (element.clas ...

  9. Vue子组件调用父组件方法并传参的5种方式:$emit触发、传入子组件function、访问父组件$parent.function、用inject关联父组件provide的方法、用window.fun

    如需了解老子怎么控制儿子的,传送门:https://s-z-q.blog.csdn.net/article/details/119922715 子组件child.vue <template> ...

  10. Vue父组件调用子组件的方法并传参的两种方式(用$refs.refName.functionName、window.function)

    如需了解儿子怎么控制老子的,传送门:https://s-z-q.blog.csdn.net/article/details/120094689 父组件father.vue <template&g ...

最新文章

  1. 隔壁,阿里18k老测试员常用的 软件测试工具大全
  2. iOS UINavigationController
  3. php redis mset,MSET命令_视频讲解_用法示例-redis编程词典-php中文网
  4. 小白开学Asp.Net Core 《八》
  5. 【Kaggle】Intermediate Machine Learning(XGBoost + Data Leakage)
  6. 关于 VM Linux操作系统使用 360随身wifi的驱动安装问题
  7. mybatis case when_MyBatis 几种通用的写法
  8. MongoDB在本地安装与启动
  9. spring mvc controller间跳转 重定向 传参 (转)
  10. 单出口双防火墙双核心冗余_各类冗余备份技术合集
  11. java使用itext导出pdf,图片、表格、背景图
  12. 考贵大计算机专业研究生,贵州大学(专业学位)计算机技术考研难吗
  13. 企业微信自建应用调试方法以及小坑
  14. 计算机图形学流体仿真mac网格,正交网格下不可压缩流体的图形学模拟
  15. STM32开发基础知识——OLED开发基础
  16. 解决2K 显示器的尴尬为MacBook 开启HiDPI(新方法支持M1)
  17. 使用python实现微信小程序自动签到2.0
  18. matlab GUI 绘图 坐标轴控件
  19. 编写一个算法,将非负的十进制整数转换为其他进制的数输出,10及其以上的数字从‘A’开始的字母表示。
  20. 喜闻乐见ELK(日志管理分析系统)

热门文章

  1. 第十六周程序阅读(3)
  2. 你所不知道的 Android Studio 调试技巧
  3. JVM中垃圾收集算法
  4. android自定义WaveView水波纹控件
  5. 最重要的事情只有一件
  6. css实现横向进度条和竖向进度条
  7. VI打开和编辑多个文件的命令
  8. Java 使用ZeroMQ 2.2 进行通信编程
  9. NDC 2010视频下载:看看其他微软平台程序员们都在做什么
  10. GridView 模版列编辑状态Dropdownlist 事件