[英]Case expression in json_build_object()
I want to add a case expression inside of my json_build_object for the times where and endtime
has not yet been set. 我想在json_build_object内部添加一个case表达式,以设置尚未设置的时间和endtime
时间。
CODE 码
Current 当前
SELECT json_build_object(
'endtime', json_agg(CONCAT(endtime::timestamp::date, ' ', endtime::timestamp(0)::time )),
'starttime', json_agg(CONCAT(starttime::timestamp::date, ' ', starttime::timestamp(0)::time )),
'totaltime', json_agg(starttime::timestamp - endtime::timestamp),
)
FROM jobs
Expected 预期
SELECT json_build_object(
'endtime', json_agg(CONCAT(endtime::timestamp::date, ' ', endtime::timestamp(0)::time )),
'starttime', json_agg(CONCAT(starttime::timestamp::date, ' ', starttime::timestamp(0)::time )),
CASE WHEN endtime IS NULL
THEN 'totaltime', json_agg(starttime::timestamp - NOW()::timestamp)
ELSE 'totaltime', json_agg(starttime::timestamp - endtime::timestamp)
END
)
FROM jobs
RESULT 结果
Current 当前
{
"endtime" : [" ", "2019-07-22 18:50:06", "2019-07-19 19:24:13", "2019-07-19 16:23:46"],
"starttime" : ["2019-07-24 16:02:49", "2019-07-22 20:12:01", "2019-07-19 16:55:55", "2019-07-19 14:56:48"],
"totaltime" : [null, "01:21:55.150273", "-02:28:17.795901", "-01:26:57.872932"]
}
Expected 预期
When the endtime
is null I would like like the totaltime
to be the time till now 当endtime
为空,我想喜欢totaltime
是时间到现在
{
"endtime" : [" ", "2019-07-22 18:50:06", "2019-07-19 19:24:13", "2019-07-19 16:23:46"],
"starttime" : ["2019-07-24 16:02:49", "2019-07-22 20:12:01", "2019-07-19 16:55:55", "2019-07-19 14:56:48"],
"totaltime" : ["01:2..timeTillNow", "01:21:55.150273", "-02:28:17.795901", "-01:26:57.872932"]
}
Because you're using aggregated functions, if you put the case when endtime
around the json_agg
, you're checking the value of the aggregated endtime
and it must be in a group by
. 因为您使用的是汇总函数,所以如果将case when endtime
时间放在json_agg
周围,则您要检查汇总endtime
的值,并且该值必须在group by
。
Instead, just like the concat
, the case
goes inside the json_agg
. 相反,就像concat
,该case
位于json_agg
内部。 This expression will be evaluated for each row to provide json_agg
with the values to aggregate. 将针对每一行评估该表达式,以向json_agg
提供要聚合的值。
SELECT json_build_object(
'endtime', json_agg(
concat(endtime::timestamp::date, ' ', endtime::timestamp(0)::time )
),
'starttime', json_agg(
concat(starttime::timestamp::date, ' ', starttime::timestamp(0)::time )
),
'totaltime', json_agg(
case when endtime is null
then starttime::timestamp - NOW()::timestamp
else starttime::timestamp - endtime::timestamp
end
)
)
FROM jobs;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.