json列postgres的sum元素

json列postgres的sum元素,json,postgresql,Json,Postgresql,我试图对过去24小时内json列中特定类型的所有元素求和,但我无法理解我的查询 因此,我有一个名为data的json列,我希望“live”键包含过去24小时内所有“live”的总和,为此我尝试这样做 select sum(data->>'live'::json) from probing where device_id='f051b333-8f1f-4e65-9acc-e76470a87f47' and timestamp > current_timestamp -

我试图对过去24小时内json列中特定类型的所有元素求和,但我无法理解我的查询

因此,我有一个名为data的json列,我希望“live”键包含过去24小时内所有“live”的总和,为此我尝试这样做

select sum(data->>'live'::json) 
from probing 
where device_id='f051b333-8f1f-4e65-9acc-e76470a87f47' 
  and timestamp > current_timestamp - interval '1 day' 
order by timestamp desc;

我知道这是行不通的,我想不出一个可行的解决办法。我看到了一些解决方案,包括使用“json\u数组元素”或“json\u每个元素”,但无法实现这一点。

我提供了这个解决方案:

select 
    sum(cast(data->>'live' as integer)) 
from 
    probing 
where 
    device_id='f051b333-8f1f-4e65-9acc-e76470a87f47' 
    and timestamp > current_timestamp - interval '1 day';

您可以通过以下两种方式进行查询:

select sum((data->>'live')::int) 
from probing 
where device_id='f051b333-8f1f-4e65-9acc-e76470a87f47' 
and timestamp > current_timestamp - interval '1 day' 
order by timestamp desc;
select sum(cast(data->>'live' as int)) 
from probing 
where device_id='f051b333-8f1f-4e65-9acc-e76470a87f47' 
and timestamp > current_timestamp - interval '1 day' 
order by timestamp desc ;