[英]Getting derived column from database | SQL query | DB2 SystemDate
My table is PRODUCTINFO
: 我的表是
PRODUCTINFO
:
Column name : product_name | launch_date
Sample Date : product1 2017-01-20
I need a SQL query to decide if the product is newly launched or not. 我需要一个SQL查询来确定该产品是否是新推出的。
Business rule : 业务规则:
if (launch_date - currentdate < 0 && currentdate - launch_date < 90)
newly_launched = 'YES'
else
newly_launched = 'NO'
where currentdate is today's date. 当前日期是今天的日期。
SQL query I am witting is like : 我想知道的SQL查询是这样的:
SELECT launch_date, X as newly_launched
FROM PRODUCTINFO
WHERE product_name = 'product1'
I am not able to figure out correct replacement of 'X' in my query for desired result. 我无法在查询中找出正确替换'X'的结果。
Problem I am facing is using currentdate and if else block in my query 我面临的问题是使用currentdate,如果在我的查询中阻塞
Please help. 请帮忙。
One way to get currentdate in DB2 using following query : 使用以下查询在DB2中获取currentdate的一种方法:
SELECT VARCHAR_FORMAT(CURRENT TIMESTAMP, 'YYYYMMDD')
FROM SYSIBM.SYSDUMMY1
Still not sure how to use this with my if else scenario. 仍然不确定如何在其他情况下使用此功能。
Answer here don't solve my problem as this scenario is if-else based. 在这里回答并不能解决我的问题,因为这种情况是基于if-else的。
use TIMESTAMPDIFF with 16 as first parameter for count number days between 2 timestamps, for more detail look here 将TIMESTAMPDIFF与16作为第一个参数,用于计算两个时间戳之间的天数计数,更多详细信息请参见此处
try this 尝试这个
select launch_date,
case when TIMESTAMPDIFF( 16, cast(cast(launch_date as timestamp) - current timestamp as char(22))) >0 and
TIMESTAMPDIFF( 16, cast(cast(launch_date as timestamp) - current timestamp as char(22))) <90 then 'YES' else 'NO' end newly
from PRODUCTINFO
WHERE product_name = 'product1'
simply use datediff function in a case syntax, I hope it could help! 只需在大小写语法中使用datediff函数,希望对您有所帮助!
SELECT launch_date,
case
when DATEDIFF(day,launch_date,getdate()) BETWEEN 0 AND 90 then 'YES'
else 'NO'
end AS newly_launched
FROM PRODUCTINFO
WHERE product_name = 'product1'
in case you want to do it for all your records: 如果您想为所有记录做这些事情:
SELECT launch_date,
case
when DATEDIFF(day,launch_date,getdate()) BETWEEN 0 AND 90 then 'YES'
else 'NO'
end AS newly_launched
FROM PRODUCTINFO
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.