我通常使用这种语法按天分组:
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日期,计数(... ^提示:没有函数匹配给定的名称和参数类型。您可能需要添加显式类型转换。
有没有办法让这项工作?
答案 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;