我正在尝试比较由update触发器填充的历史记录表中的值,以查看JSON字段的旧值和新值中的某些列是否相等,如果所有列均相等,则可以在查询时创建案例。这是我想要做的:
SQL
create table history
(
id serial not null,
ts timestamp default now(),
table_schema text,
table_name text,
operation text,
updated_by text default CURRENT_USER,
new json,
old json
);
With t AS (
select id,
old->>'field1' = new->>'field1' as isMatchField1,
old->>'field2' = new->>'field2' as isMatchField2,
old->>'field3' = new->>'field3' as isMatchField3,
old->>'field4' = new->>'field4' as isMatchField4
from history)
select id, array [isMatchField1, isMatchField2, isMatchField3, isMatchField4] from t
输出
1,{true,true,false,null}
如何从数组中筛选出所有空值,并在查询时进行案例分析以查看是否仅存在真值。基本上我想做类似的事情:
select id,
case when array field is only true and null then 'no changes made'
else
'changes made'
end as updated
from t