I want to plot avg of a value picked from a nested json which is stored as jsonb object in a postgres column. Following query works for me in Postgres but I am not able to figure out how to use this in grafana for time series plotting:
Postgres Query:
SELECT timestamp::TIMESTAMP::Date as date , http.key AS http_method , hostname , s.key AS status_code , avg((s.value->>'count')::int) AS avg_count FROM public.metrics t , jsonb_each(t.metrics->'http') http , jsonb_each(http.value->'statusCode') s GROUP BY date, hostname, http.key, s.key;
I tried following in Grafana:
SELECT $__time(timestamp) , hostname as metric , avg((s.value->>'count')::int) AS value , http.key , s.key AS status_code FROM public.metrics t , jsonb_each(t.metrics->'http') http , jsonb_each(http.value->'statusCode') s WHERE $__timeFilter(timestamp) and hostname like '%ABC%' group by time, hostname, http.key, s.key order by time
Thanks in advance for providing any guidance