Registro de declaraciones de PostgreSQL

1. Crea un índice

-- 创建索引
CREATE SEQUENCE user_ID_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;

-- 应用索引
alter table user alter column id set default nextval('user_ID_seq');

2. Genera fechas consecutivas

-- 生成连续日期
SELECT
	daytime::date date_time 
FROM
	generate_series ( ( SELECT ( now( ) + INTERVAL '-1 month' ) :: DATE ), ( SELECT now( ) :: DATE ), '1 day' ) AS daytime
order by date_time;

3. La función date_trunc(), que se usa para truncar una marca de tiempo o un intervalo de tiempo a un nivel de precisión específico

-- 获取上月月初日期
select date_trunc('month', now() + '-1 months')::date

-- 获取月初日期
select date_trunc('month', now())

-- 查询上个月数据
date >= date_trunc('month', now() + '-1 months')::date
date < date_trunc('month', now())::date

-- 每月1号 12点
select date_trunc('month',now()) +interval '12 h';

-- 每月15号9点半
select date_trunc('month',now()) + interval '15 d 9 h 30 min';

-- 每天9点	
select date_trunc('day',now()) + interval '9 h';

-- 每周的今天
select date_trunc('day',now()) + interval '7 d';

-- 每周的周二第一分钟
select date_trunc('weak',now())  + interval '1d 1minute';

-- 每小时
select date_trunc('h',now()) + interval '30 minute';

-- 每分钟
select date_trunc('minute',now()) + interval '30 second';

-- 每30分钟
select date_trunc('minute',now()) + interval '30 minute 30 second';

-- 本季度的第15天,15小时 15分 30秒
select date_trunc('quarter',now()) + interval '15 d 15 h 15 minute 30 second';

-- 每个季度最后一天的晚上11点
select date_trunc('quarter',now() ) - interval '1 h';

-- 每个季度的最后一天的晚上的11点(从下个季度开始算起)
select date_trunc('quarter',now() + interval '3 month') - interval '1 h';

4. Genere fechas continuas y combine tipos específicos, es decir, la misma fecha corresponde a múltiples tipos de filas

-- 将生成的连续日期和类型进行组合成一张表(方法:将日期和类型表进行合并为以逗号拼接成字符串,然后将字符串分割成多行)
SELECT
	dt.date_time,
	UNNEST ( string_to_array( dt."type", ',' ) ) "type"
FROM
	(
	SELECT
		to_char( daytime, 'yyyy-MM-dd' ) date_time,
		( SELECT string_agg ( "type", ',' ) FROM ( SELECT DISTINCT "type" FROM table_1 ) ty ) "type" 
	FROM
		generate_series ( ( SELECT ( now( ) + INTERVAL '-1 month' ) :: DATE ), ( SELECT now( ) :: DATE ), '1 day' ) AS daytime 
	ORDER BY
		date_time 
	) dt

5,

Supongo que te gusta

Origin blog.csdn.net/fengxing_2/article/details/127276557
Recomendado
Clasificación