我有一个名为info的JSON列,其中包含以下值:
{"width":"800, "height":"480"}
{"width":"800, "height":"480"}
{"width":"768, "height":"480"}
我需要一个SQL查询来获得这种格式的输出:
Label |Count
==============
800x480, 2
768x480, 1
第二个值代表计数。
我设法做的是提取第一个值和计数:
select info->>'width' as label, count(info->>'width') from table_name GROUP BY 1 ORDER BY 2 DESC LIMIT 5
哪个输出:
Label |Count
==============
800, 2
768, 1
答案 0 :(得分:1)
常规string concatenation应该这样做。出于某些原因,如果从查询中省略()
,查询将失败。
SELECT (info->>'width') || 'x' || (info->>'width') AS label,
COUNT(info->>'width')
FROM table_name
GROUP BY 1
ORDER BY 2 DESC
LIMIT 5