gpt4 book ai didi

MySQL:基于日期范围的计算列

转载 作者:行者123 更新时间:2023-11-30 22:25:42 25 4
gpt4 key购买 nike

如何创建一个计算列,如果日期在 2015 年以内则返回值 1,否则返回 0?

我昨天用谷歌搜索和搜索 StackOverflow 寻找解决方案,但无济于事。根据我对 CASE WHEN、BETWEEN 和 CAST 的了解,我将以下 SQL 脚本放在一起。

然而,尽管在 2015 年、2016 年有相应的 Prescreen 日期,甚至日期为 0000-00-00,但下面的(截断的)脚本会产生全 0,如下所示: enter image description here

SELECT 
table_r.R_Number,
table_c.ref_num,
DATE(STR_TO_DATE(table_c.Prescreen, '%d-%b-%y')) AS Prescreen,
CASE
WHEN Prescreen BETWEEN CAST('2015-01-01' AS DATE) AND CAST('2015-12-31' AS DATE) THEN 1
ELSE 0
END AS YTD2015_Prescreen,
table_r.Region,
FROM
table_c
INNER JOIN
table_r ON table_c.R_Number = table_r.R_Number
WHERE
table_c.Int <> ''
;

我谦虚地请求你把你的天才借给我。谢谢。 :)

最佳答案

试试这个:

    SELECT 
table_r.R_Number,
table_c.ref_num,
STR_TO_DATE(table_c.Prescreen,'%d-%b-%y') AS Prescreen,
CASE
WHEN STR_TO_DATE(table_c.Prescreen,'%d-%b-%y') BETWEEN CAST('20150101'AS DATE) AND CAST('20151231' AS DATE) THEN 1
ELSE 0
END AS YTD2015_Prescreen,
table_r.Region,
FROM
table_c
INNER JOIN
table_r ON table_c.R_Number = table_r.R_Number
WHERE
table_c.Int <> ''
;

关于MySQL:基于日期范围的计算列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35301990/

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