赞
踩
记录PostgreSQL的常用函数及其在实际工作中的应用,持续更新中。
STRING_AGG(xxx, ‘间隔符’)
SELECT t1.mdm_code AS new_material_code ,COALESCE(STRING_AGG(t2.old_product_code, ','), '无') AS old_material_code ,t1.name AS material_name ,t1.active AS active_flag FROM ( SELECT id ,mdm_code ,name ,active FROM ods_xxx WHERE mdm_code != 'A0010000-ZZZZZZ' AND mdm_code IS NOT NULL ) t1 LEFT OUTER JOIN ( SELECT old_product_code ,new_product_code FROM ods_yyy ) t2 ON t1.mdm_code = t2.new_product_code
TO_CHAR(xxx, ‘yyyy-MM-dd’)
TO_CHAR("FDate", 'yyyy-MM-dd') AS arrange_date
(一)SPLIT_PART(xxx, ‘根据某部分截取’, 取第几部分)
SPLIT_PART("FSalerNumber", '_', 1) AS saler_code
(二)SUBSTRING(xxx, 第几位, 到第几位)
SELECT substring('123',1,2)
GRANT SELECT ON TABLE 表名 TO 用户名
字符串类型:VARCHAR
小数类型:NUMERIC(xx,x)
整数类型:INTEGER
日期类型:DATE
DATE_PART(‘day’,‘2022-02-05’::timestamp-‘2022-02-01’::timestamp)
SELECT DATE_PART('day','2022-02-05'::timestamp - '2022-02-01'::timestamp)
CONCAT()
SELECT CONCAT('jojo','bobo')
alter table 表名 rename column 原字段名 to 修改后字段名
select '2022-01-01'::timestamp + '1 day'
select '2022-01-01'::timestamp + '1 month'
select '2022-01-01'::timestamp + '1 year'
DROP TABLE IF EXISTS xxx.test
(
id INT NOT NULL
,name VARCHAR(50)
,amt NUMERIC(10,2)
,PRTMARY KEY (id)
);
COMMENT ON TABLE xxx.test IS '测试表';
COMMENT ON COLUMN xxx.test.id IS 'ID';
COMMENT ON COLUMN xxx.test.name IS '名称';
COMMENT ON COLUMN xxx.test.amt IS '金额';
-- ALTER TABLE 表名 ALTER COLUMN 字段名 TYPE 修改后字段类型;
ALTER TABLE test.ads_xxx ALTER COLUMN pay_time TYPE TIMESTAMP;
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。