Hello all,
So I’ve been working on this query:
SELECT custom_key, ARRAY_AGG(p) AS products FROM `BucketXXX` AS p
LET custom_key = p.defaultName || "|" || p.brand.id || "|" || p.externalSpec
WHERE p.type = 'product'
AND p.deleted = false
GROUP BY custom_key
It is working fine and the result query is like this:
[
{
products: [
{
id: "px0",
defaultName: "product X",
brand: { name: "brand 00", id: "brand00-00" },
externalSpec: "spec01",
suppliers: [ { id: "sup00", name: "Supplier Pope" }, { id: "sup99", name: "Supplier Jhon" , { id: "sup01", name: "Supplier Pope" }} ]
},
],
unique_key: "product X|brand00-00|spec01"
},
{
products: [
{ defaultName: "product Y", brand: null, externalSpec: "spec01", suppliers: [] }
],
unique_key: "product Y||spec01"
},
{
products: [
{ defaultName: "product LL", brand: { name: "brand 01", id: "brand00-01" }, externalSpec: "spec00", suppliers: null },
],
unique_key: "product LL|brand00-01|spec00"
},
]
Now I need to perform some operations on this result in order to return a true or false value…
SELECT RAW
CASE
WHEN COUNT(repeated _ products) == 0 THEN TRUE
WHEN COUNT(repeated_products) > 1 THEN FALSE
WHEN
COUNT(repeated_products) == 1
AND repeated_products[0] IS NOT NULL
AND repeated_products[0].products[0] IS NOT NULL
AND repeated_products[0].products[0].suppliers IS NOT NULL
AND
COUNT(SELECT suppliers.id FROM repeated_products[0].products[0].suppliers AS suppliers GROUP BY suppliers.id ) > 1 THEN FALSE
ELSE TRUE
END
FROM (SELECT custom_key, ARRAY_AGG(p) AS products FROM `BucketXXX` AS p
LET custom_key = p.defaultName || "|" || p.brand.id || "|" || p.externalSpec
WHERE p.type = 'product'
AND p.deleted = false
GROUP BY custom_key) AS repeated_products
For that case the two first cases seems to be working fine but the more last elaborated case where I want to check if the supplier is repeated by grouping is giving an error code 3000, any help would be appreciated,
Thanks