gpt4 book ai didi

sql - 搜索 holiday doy IN(字符串)(Postgresql)

转载 作者:行者123 更新时间:2023-11-29 13:17:17 24 4
gpt4 key购买 nike

这项工作:

WITH month AS ( 
SELECT date_part('doy',d.dt) as doy,
dt::date as date
FROM generate_series('2017-01-01','2017-01-15', interval '1 day') as d(dt)
)

SELECT date,
CASE
WHEN doy IN (1,2,3) THEN 0 ELSE 8 END
FROM month

http://sqlfiddle.com/#!15/aed15/10

但是如果我将 1,2,3 存储为字符串

CREATE TABLE holidays
(id int4,days character(60));
INSERT INTO holidays
(id,days)
VALUES
('2017','1,2,3');

...并用以下字符串替换 1,2,3:

WITH month AS ( 
SELECT date_part('doy',d.dt) as doy,
dt::date as date
FROM generate_series('2017-01-01','2017-01-15', interval '1 day') as d(dt)
)

SELECT date, days,
CASE
WHEN doy::text IN (days) THEN 0 ELSE 8 END
FROM month
LEFT JOIN holidays ON id=2017

http://sqlfiddle.com/#!15/aed15/13

似乎“天”没有正确类型转换。但我想不通。

TIA,

最佳答案

此处最短的解决方案是将字符串列表转换为数组并使用ANY 构造:

WITH month AS ( 
SELECT date_part('doy',d.dt) as doy,
dt::date as date
FROM generate_series('2017-01-01','2017-01-15', interval '1 day') as d(dt)
)

SELECT date, days,
CASE
WHEN doy::text = ANY(concat('{',days,'}')::text[]) THEN 0 ELSE 8 END
FROM month
LEFT JOIN holidays ON id=2017

但我会重新考虑整个解决方案,因为感觉不对

关于sql - 搜索 holiday doy IN(字符串)(Postgresql),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47250352/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com