PL/pgSQL按字段对两个JSON或两行进行求和

PL/pgSQL按字段对两个JSON或两行进行求和,sql,postgresql,plpgsql,Sql,Postgresql,Plpgsql,我花了几个小时试图找到问题的答案,但我没有: 我想将某些行(具有相同日期和'name'值)的所有值相加为另一行,该行将成为'aggregation one' 为此,我选择所需的行,按日期/名称对所有行执行循环,并对“行”字段执行最后一个循环,以逐个对字段求和: DO $code$ DECLARE date_trunc_by_hour record; row_for_hour record; field_of_row_for_hour record; aggreged metrics.bot

我花了几个小时试图找到问题的答案,但我没有:

我想将某些行(具有相同日期和'name'值)的所有值相加为另一行,该行将成为'aggregation one'

为此,我选择所需的行,按日期/名称对所有行执行循环,并对“行”字段执行最后一个循环,以逐个对字段求和:

DO $code$

DECLARE 
date_trunc_by_hour record; 
row_for_hour record;
field_of_row_for_hour record;
aggreged metrics.bot%ROWTYPE;
first boolean := true;
BEGIN

--boucle par heure, loop by hour
FOR date_trunc_by_hour IN
SELECT date_trunc('hour',date) as date_trunc
FROM metrics.bot 
where aggrege = false 
group by date_trunc
LOOP
 RAISE INFO 'Recherche pour la date : %', date_trunc_by_hour.date_trunc;
--boucle par row dans l'heure, loop on each row for the hour selected
first := true ;
FOR row_for_hour IN
  SELECT *
    FROM metrics.bot 
    where date_trunc('hour',date) = date_trunc_by_hour.date_trunc       

    LOOP
    IF first = true 
        THEN
        aggreged := row_for_hour; -- if it's the first row, i take the values of this row to add ones of the others rows after
        RAISE INFO '%', aggreged;
        first = false;
        ELSE -- if its not the first, i want to add values field by field to the first one
            FOR field_of_row_for_hour IN select * from json_each(row_to_json(row_for_hour))
            LOOP

        -- Here i would like for each field of each row in the loop, to add incoming row value to aggreged value, value can be a json                                                                

            END LOOP;

    END IF;
    END LOOP;

END LOOP;

END $code$;
例如,具有相同bot/日期对的前两行应给出第三行:

 ===== ===================== ========= ========= ===== ============================ ========== 
  BOT          DATE           PROCESS   INSERT    MAJ             ERRORS             aggreged  
 ===== ===================== ========= ========= ===== ============================ ========== 
    1   2019-02-12 17:00:00   scan            2     5   {"société":1}                false     
    1   2019-02-12 17:00:00   scan            4     7   {"société":1,"enchere":1}    false     
    1   2019-02-12 17:00:00   scan            6    12   {"enchere":1, "société":2}   true      
 ===== ===================== ========= ========= ===== ============================ ========== 
我将在删除第1行和第2行之前插入到表中

在我的记忆中,有一次我用json转换解决了这个问题,但我不记得是怎么解决的


感谢您的帮助

您需要一个对整数属性求和的自定义聚合:

create or replace function jsonb_sum_attributes(jsonb, jsonb)
returns jsonb language sql as $$
    select jsonb_object_agg(key, sum)
    from (  
        select key, sum(value::int)
        from (
            select *
            from jsonb_each_text($1)
            union all
            select *
            from jsonb_each_text($2)
            ) s
        group by key
        ) s
$$;

create aggregate jsonb_sum_attributes_agg(jsonb) 
(  
    sfunc = 'jsonb_sum_attributes',
    stype = jsonb,
    initcond = '{}'
);
查询:

select bot, date, process, insert, maj, errors, aggreged
from bot
union 
select bot, date, process, sum(insert), sum(maj), jsonb_sum_attributes_agg(errors), true
from bot
group by bot, date, process
order by bot, date, process, aggreged

 bot |        date         | process | insert | maj |            errors            | aggreged 
-----+---------------------+---------+--------+-----+------------------------------+----------
   1 | 2019-02-12 17:00:00 | scan    |      2 |   5 | {"société": 1}               | f
   1 | 2019-02-12 17:00:00 | scan    |      4 |   7 | {"enchere": 1, "société": 1} | f
   1 | 2019-02-12 17:00:00 | scan    |      6 |  12 | {"enchere": 1, "société": 2} | t
(3 rows)