【发布时间】:2018-01-24 10:40:44
【问题描述】:
我有以下疑问:
SELECT
distinct(date(survey_results.created_at)),
json_build_object(
'high',
ROUND(
COUNT(*) FILTER (WHERE ( scores#>>'{medic,categories,motivation}' in('high', 'medium'))) OVER(order by date(survey_results.created_at) ) * 1.0 /
(
CASE (COUNT(*) FILTER (WHERE (scores#>>'{medic,categories,motivation}' in('high','medium','low'))) OVER(order by date(survey_results.created_at)))
WHEN 0.0 THEN 1.0
ELSE (COUNT(*) FILTER (WHERE (scores#>>'{medic,categories,motivation}' in('high','medium','low'))) OVER(order by date(survey_results.created_at)))
END)* 100, 2 ) ) AS childcare FROM survey_results GROUP BY date, scores ORDER BY date asc;
问题在于使用distinct(date(survey_results.created_at))。就地查询返回错误:
could not identify an equality operator for type json
这是显示该问题的 db fiddle:
https://www.db-fiddle.com/f/vUBjUyKDUNLWzySHKCKcXA/1
我该如何解决这个问题?
【问题讨论】:
-
distinct是 NOT 一个函数。它始终适用于结果的 所有 列。distinct(a),b与distinct a, (b)或distinct a, b相同。正因为如此,distinct尝试比较您的第二列的相同值,该列的类型为json,并且无法与=进行比较 -
知道了,这个查询返回了一个重复的日期,我怎样才能在不使用 distinct 的情况下修复它?
标签: sql json postgresql