I have two tables with a one-to-many relationship that looks something like this:
CREATE TABLE products (
id UUID PRIMARY KEY,
name TEXT
);
CREATE TABLE product_variations (
id UUID PRIMARY KEY,
product_id UUID REFERENCES products,
attribute1 TEXT,
attribute2 TEXT,
attribute3 TEXT
);
I’d like to create a view which finds all the attributes that are the same for all variations of a product. I’m guessing it would look something like this:
CREATE VIEW product_shared_attributes AS
SELECT "product_id", some_aggregator("attribute1"), some_aggregator("attribute_2")
FROM product_variations
GROUP BY "product_id";
What kind of aggregator could I use for this? I would expect it to return the value if all values are the same, and NULL otherwise.
2
Answers
you can use
STING_AGG
You could count the number of distinct values:
You could use
max
instead ofmin
, but you need an aggregate function. TheCASE
expression results in NULL if none of the conditions apply and there is noELSE
branch.