我有一个CASE语句,如下所示
case
when ph.term_start_date > x.year1 then 0
DATEDIFF(case when ph.term_end_date > x.year1 then x.year1 else ph.term_end_date end, greatest(x.report_date, ph.term_start_date))
end days_remaining_year1,
case
when ph.term_start_date > x.year1 then 0
DATEDIFF(case when ph.term_end_date > x.year1 then x.year1 else ph.term_end_date end, greatest(x.report_date, ph.term_start_date))
end *(ph.amount + ifnull(y.credit,0))/DATEDIFF(ph.term_end_date, ph.term_start_date) as deferral_year1,
我需要了解最后一行*
关键字之后END
的含义。我认为在结束关键字之后你必须指定一个将出现在结果集
Case
When ( something )
end ( return column )
答案 0 :(得分:2)
*
是乘法运算符
Doc:MySQL Arithmetic Operators
前面的CASE
返回一个值,然后乘以:
(ph.amount + ifnull(y.credit,0))/DATEDIFF(ph.term_end_date, ph.term_start_date)
此结果将在deferral_year1
列
编辑 :回复评论
我偷偷摸摸你的2 CASE
个陈述,他们看起来对我不正确。您的查询是否正确执行?
看起来在DATEDIFF之前缺少else
,应该是这个IMO
case
when ph.term_start_date > x.year1 then 0
else DATEDIFF(case when ph.term_end_date > x.year1 then x.year1 else ph.term_end_date end, greatest(x.report_date, ph.term_start_date))
end
就像你说的那样ph.term_start_date > x.year1
它应该返回0, else 它会返回DATEDIFF
的结果。