Postgresql tiene la función ARRAY_REMOVE para eliminar elementos de una matriz. ¿Cómo puedo hacer esto en Snowflake?
El enfoque UDF funciona bien, aquí hay otro enfoque si desea:
Dependiendo del caso de uso ... Si no es probable que se beneficie del almacenamiento en caché y desea un código de aspecto 'más limpio', entonces el UDF podría ser mejor; sin embargo, si es probable que se beneficie del almacenamiento en caché, prefiera solo SQL, entonces tal vez este enfoque pueda sé útil.
FLATTEN the ARRAY -> Predicate data -> Return via ARRAY_AGG
SELECT ARRAY_AGG(VALUE) REMOVED FROM CTE, LATERAL FLATTEN(AN_ARRAY) WHERE VALUE!='A'Los mismos ejemplos funcionan bien.
select array_agg(value) from table(flatten(input => parse_json('["a", "b", "c", "a"]'))) where value not in ('a'); select array_agg(value) from table(flatten(input => parse_json('[4, 1, 4, 2, 3, 4]'))) where value not in (4); select array_agg(value) from table(flatten(input => parse_json('[4.3, 1.1, 4.2, 0.1, 3.3, 0.2]'))) where value not in (0.2);Puede crear una UDF JS. Para que funcione con cualquier tipo, deberá convertir el valor que se eliminará en la variante:
CREATE OR REPLACE FUNCTION array_remove_js(ARR variant, VAL variant) returns array language javascript as ' return ARR.filter(function(item) { return item !== VAL }) ';Probando con cadenas, enteros y flotantes:
select array_remove_js(parse_json('["a", "b", "c", "a"]'), 'a'::variant); select array_remove_js(parse_json('[4, 1, 4, 2, 3, 4]'), 4::variant); select array_remove_js(parse_json('[4.3, 1.1, 4.2, 0.1, 3.3, 0.2]'), 0.2::variant);