如何使用JSONB值的日期/时间函数?

时间:2018-03-12 23:32:38

标签: postgresql jsonb

我通常使用这种语法按天分组:

SELECT date_trunc('day', loggedin) AS "Day" , count(*) AS "No. of users"
FROM logins
WHERE created > now() - interval '3 months' 
GROUP BY 1 
ORDER BY 1;

现在我的Date-value位于名为extras的JSONB列中。

SELECT date_trunc('day', "extras"->>'sent') AS date , count(*) AS "value"
FROM "document"
GROUP BY 1 
ORDER BY 1;

这会出现此错误:

  

错误:函数date_trunc(unknown,text)不存在第1行:   选择date_trunc(' day'," extras" - >>'发送')AS日期,计数(...                  ^提示:没有函数匹配给定的名称和参数类型。您可能需要添加显式类型转换。

有没有办法让这项工作?

1 个答案:

答案 0 :(得分:2)

extras->>'sent'的文字投放到date

SELECT date_trunc('day', ("extras"->>'sent')::date) AS date , count(*) AS "value"
FROM "document"
GROUP BY 1 
ORDER BY 1;