postgresql

pg中提取字符串中字段

SELECT
    (regexp_matches('{"newValue":"字数为15个字的用例自定义状态","oldKey":"0","fieldName":"statusName","newKey":"118","oldValue":"新建"}', '"oldKey":"([^"]*)"'))[1] AS old_key,
    (regexp_matches('{"newValue":"字数为15个字的用例自定义状态","oldKey":"0","fieldName":"statusName","newKey":"118","oldValue":"新建"}', '"fieldName":"([^"]*)"'))[1] AS field_name,
    (regexp_matches('{"newValue":"字数为15个字的用例自定义状态","oldKey":"0","fieldName":"statusName","newKey":"118","oldValue":"新建"}', '"oldValue":"([^"]*)"'))[1] AS old_value,
    (regexp_matches('{"newValue":"字数为15个字的用例自定义状态","oldKey":"0","fieldName":"statusName","newKey":"118","oldValue":"新建"}', '"newKey":"([^"]*)"'))[1] AS newKey,
    (regexp_matches('{"newValue":"字数为15个字的用例自定义状态","oldKey":"0","fieldName":"statusName","newKey":"118","oldValue":"新建"}', '"newValue":"([^"]*)"'))[1] AS newValue

查看进程

SELECT *
FROM pg_stat_activity
WHERE state = 'active'
;

一行转一列,类似侧视图功能

select
            regexp_split_to_table(
               '6992560,6992506,6992505,6992504,6992503,6992502,6992469,6992501,6992468,6992500,6992467,6992466,6992465,6992464,6992463,6992462,6992461,6992509,6992508,6992507,6992517,6992516,6992515,6992119,6992514,6992118,6992513,6992117,6992512,6992116,6992479,6992511,6992115,6992478,6992510,6992114,6992477,6992113,6992476,6992112,6992475,6992111,6992474,6992473,6992472,6992471,6992470,6992519,6992518,6992528,6992527,6992526,6992525,6992129,6992524,6992128,6992523,6992127,6992489,6992522,6992126,6992488,6992521,6992125,6992487,6992520,6992124,6992486,6992123,6992122,6992485,6992121,6992484,6992120,6992483,6992482,6992481,6992480,6992490,6992529,6992539,6992538,6992537,6992536,6992535,6992534,6992533,6992499,6992532,6992498,6992531,6992135,6992497,6992530,6992134,6992496,6992133,6992495,6992132,6992494,6992131,6992493,6992130,6992492,6992491,6992549,6992548,6992547,6992546,6992545,6992544,6992543,6992542,6992541,6992540,6992559,6992558,6992557,6992556,6992555,6992554,6992553,6992552,6992551,6992550'
                , ',')

一列转一行,需要分组(组内 一列转一行)

类似hive中
select id,concat_ws(',',collect_list(name)) names from ts group by id;

pg

WITH temp_table(id, num, name) AS (
  VALUES
    (1, 'zs', '合肥'),
    (1, 'ls', '南京'),
    (1, 'ww', '杭州'),
    (1, 'zl', '重庆'),
    (1, 'sq', '郑州'),
    (2, 'wb', '六安'),
    (2, 'lq', '青岛'),
    (3, 'dd', '三亚'),
    (3, 'si', '常州'),
    (3, 'sh', '武汉')
)
SELECT id,string_agg(name,',')
FROM temp_table
group by id;
相关推荐
苹果醋312 分钟前
React源码02 - 基础知识 React API 一览
java·运维·spring boot·mysql·nginx
晓纪同学28 分钟前
QT-简单视觉框架代码
开发语言·qt
威桑28 分钟前
Qt SizePolicy详解:minimum 与 minimumExpanding 的区别
开发语言·qt·扩张策略
Hello.Reader31 分钟前
深入解析 Apache APISIX
java·apache
飞飞-躺着更舒服31 分钟前
【QT】实现电子飞行显示器(简易版)
开发语言·qt
了一li35 分钟前
Qt中的QProcess与Boost.Interprocess:实现多进程编程
服务器·数据库·qt
明月看潮生37 分钟前
青少年编程与数学 02-004 Go语言Web编程 16课题、并发编程
开发语言·青少年编程·并发编程·编程与数学·goweb
明月看潮生40 分钟前
青少年编程与数学 02-004 Go语言Web编程 17课题、静态文件
开发语言·青少年编程·编程与数学·goweb
Java Fans43 分钟前
C# 中串口读取问题及解决方案
开发语言·c#
日记跟新中1 小时前
Ubuntu20.04 修改root密码
linux·运维·服务器