简体   繁体   English

json_build_object()中的大小写表达式

[英]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.

相关问题 PostgreSQL json_build_object 嵌套 - PostgreSQL json_build_object nested 使用 json_agg(json_build_object(.......)) 时,如何确保 json_build_object 返回空列表 - when use json_agg(json_build_object(.......)) ,how to ensure json_build_object return empty list 如何在 Postgres 的 json_build_object() 中进行 GROUP BY 和 COUNT - How to do a GROUP BY and COUNT within json_build_object() in Postgres 如何为 json_build_object 添加默认值 postgresql - How to add a default value to json_build_object postgresql 在PostgreSQL中使用json_build_object和json_build_array排序json结构 - Ordering json structure using json_build_object and json_build_array in Postgresql 无法使用 json_build_object 从 postgreSQL 查询创建大型 json 对象 - Unable to create a large json object from a postgreSQL query using json_build_object SQL,在使用 json_agg 和 json_build_object 时遇到一些问题 - SQL, having some trouble with json_agg & json_build_object PostgreSQL Json_build_object / json_agg 添加聚合级别 - PostgreSQL Json_build_object / json_agg add level of aggregation 如何将多个 JSON_BUILD_OBJECT 条目添加到 JSON_AGG - How to Add Multiple JSON_BUILD_OBJECT entries to a JSON_AGG PostgresSQL 无法按 json_build_object 结果排序(从子查询获得) - PostgresSQL Cannot order by json_build_object result (got from subquery)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM