我有以下查询,我试图删除(
语句中第一个CREATE TABLE
之后的所有字符以及(
以仅显示表名。我有一个查询删除(
并清理表名,但我仍然无法在(
redshift_dev=# select stl.userid, trim(pg.usename) AS user,
regexp_replace(
(CASE UPPER(SPLIT_PART(text, ' ', 3))
WHEN 'IF'
THEN SPLIT_PART(text, ' ', 6)
ELSE SPLIT_PART(text, ' ', 3)
END), '(\\()','' ) AS table_name,
starttime as creation_date from stl_ddltext as stl join pg_user as pg on stl.userid = pg.usesysid where text ilike '%create table%' and pg.usename not like '%mcp_etl%' and pg.usename not like '%apiadmin%' and pg.usename not like '%afscloud%' order by creation_date desc limit 1;
userid | user | table_name | creation_date
--------+-------+--------------------------------+----------------------------
100 | admin | analytics.cluster_1000field1 | 2017-10-13 16:17:26.387716
(1 row)
期望的结果:
userid | user | table_name | creation_date
--------+-------+--------------------------------+----------------------------
100 | admin | analytics.cluster_1000 | 2017-10-13 16:17:26.387716
(1 row)
答案 0 :(得分:2)
我不确定您的create table
stmts通常是什么样的,但您可以尝试使用regexp_substr
。
SELECT
stl.userid
, trim(pg.usename) AS user
, REPLACE(regexp_substr(text, '([\.a-z0-9A-Z]*)\\('), '(', '') as tablename
, starttime AS creation_date
FROM stl_ddltext AS stl
JOIN pg_user AS pg
ON stl.userid = pg.usesysid
WHERE text ILIKE '%create table%' AND pg.usename NOT LIKE '%mcp_etl%' AND pg.usename NOT LIKE '%apiadmin%' AND pg.usename NOT LIKE '%afscloud%'
ORDER BY creation_date DESC
LIMIT 1;
它基本上尝试在第一个(
之前提取所有内容,然后替换(
。