Postgres 将假期测试解析为 table
Postgres parse vacation days test to table
我有一个 table 存储配置,我需要在查询中使用它们。
起始 table 的值存储为以下文本:
---
'1':
'1': New Year's Day
'6': Epiphany
'11':
'1': All the Saints
'12':
'24': Christmas Eve
'25': First Christmas Day
'26': Second Christmas Day
'31': New Year's Eve
'5':
'1': International Workers' Day
'8':
'15': Mid-August
例如,在第二行中,我有月份,在第三行结束时有两个 space 缩进,我有日期及其描述,用冒号分隔。
我想把它放在像这样的 table 中:
id
description
month
day
1
New Year's Day
1
1
我放了一个example in db<>fiddle.
更多黑暗正则表达式魔法将文本数据转换为 json 格式。
这样就可以通过 json 方法提取数据。
INSERT INTO holidays (holiday,description)
select
make_date(2018, l2.Month, l2.Day) as holiday
, l2.Name as description
from test
cross join lateral (
select
regexp_replace(
regexp_replace(
regexp_replace(
regexp_replace(wh,
'^\W+(\d+)\W+(\w.*)$',' {"day":"","name":""},','gm')
, '^''(\d+)'':', '{"month":"","days":[','gm')
, '\},(\s*)(?=\{"month"|$)', '}]},','g')
, '^.*?(\{.*\}).*$', '[]')::json as whjs
) l1
cross join lateral (
select
(mjs.value->>'month')::int as Month
, (djs.value->>'day'):: int as Day
, djs.value->>'name' as Name
from json_array_elements(l1.whjs) mjs
cross join lateral json_array_elements(mjs.value->'days') djs
) l2
9 rows affected
select * from holidays;
id
description
holiday
1
New Year's Day
2018-01-01
2
Epiphany
2018-01-06
3
All the Saints
2018-11-01
4
Christmas Eve
2018-12-24
5
First Christmas Day
2018-12-25
6
Second Christmas Day
2018-12-26
7
New Year's Eve
2018-12-31
8
International Workers' Day
2018-05-01
9
Mid-August
2018-08-15
在 db<>fiddle here
上测试
我有一个 table 存储配置,我需要在查询中使用它们。 起始 table 的值存储为以下文本:
---
'1':
'1': New Year's Day
'6': Epiphany
'11':
'1': All the Saints
'12':
'24': Christmas Eve
'25': First Christmas Day
'26': Second Christmas Day
'31': New Year's Eve
'5':
'1': International Workers' Day
'8':
'15': Mid-August
例如,在第二行中,我有月份,在第三行结束时有两个 space 缩进,我有日期及其描述,用冒号分隔。
我想把它放在像这样的 table 中:
id | description | month | day |
---|---|---|---|
1 | New Year's Day | 1 | 1 |
我放了一个example in db<>fiddle.
更多黑暗正则表达式魔法将文本数据转换为 json 格式。
这样就可以通过 json 方法提取数据。
INSERT INTO holidays (holiday,description) select make_date(2018, l2.Month, l2.Day) as holiday , l2.Name as description from test cross join lateral ( select regexp_replace( regexp_replace( regexp_replace( regexp_replace(wh, '^\W+(\d+)\W+(\w.*)$',' {"day":"","name":""},','gm') , '^''(\d+)'':', '{"month":"","days":[','gm') , '\},(\s*)(?=\{"month"|$)', '}]},','g') , '^.*?(\{.*\}).*$', '[]')::json as whjs ) l1 cross join lateral ( select (mjs.value->>'month')::int as Month , (djs.value->>'day'):: int as Day , djs.value->>'name' as Name from json_array_elements(l1.whjs) mjs cross join lateral json_array_elements(mjs.value->'days') djs ) l2
9 rows affected
select * from holidays;
id description holiday 1 New Year's Day 2018-01-01 2 Epiphany 2018-01-06 3 All the Saints 2018-11-01 4 Christmas Eve 2018-12-24 5 First Christmas Day 2018-12-25 6 Second Christmas Day 2018-12-26 7 New Year's Eve 2018-12-31 8 International Workers' Day 2018-05-01 9 Mid-August 2018-08-15
在 db<>fiddle here
上测试