GROUP BY clause / Aggregate function error I am having a problem creating a Table View:

DROP VIEW IF EXISTS product_variation_stock_view;

CREATE VIEW product_variation_stock_view AS
    SELECT
        product_variations.product_id AS product_id,
        product_variations.id AS product_variation_id,
        COALESCE(SUM(stocks.quantity) - COALESCE(SUM(product_variation_order.quantity), 0), 0) AS stock,
        CASE WHEN COALESCE(SUM(stocks.quantity) - COALESCE(SUM(product_variation_order.quantity), 0), 0) &gt; 0
            THEN true
            ELSE false
        END in_stock
    FROM product_variations

    LEFT JOIN (
        SELECT 
            stocks.product_variation_id AS id,
            SUM(stocks.quantity) AS quantity
        FROM stocks
        GROUP BY 
            stocks.product_variation_id
    ) AS stocks USING (id)

    LEFT JOIN (
        SELECT
            product_variation_order.product_variation_id as id, 
            SUM(product_variation_order.quantity) AS quantity
        FROM product_variation_order
        GROUP BY 
            product_variation_order.product_variation_id
    ) AS product_variation_order USING (id)


Error:

ERROR:  column "product_variations.product_id" must appear in the GROUP BY clause or be used in an aggregate function
LINE 3:   product_variations.product_id AS product_id,
      ^


Using PostgreSQL 10.3 on Ubuntu 18.04 via Laravel Homestead
I am running the query using the Postico OS X Client.
It looks like you might have to change your first few lines to reflect this:

CREATE VIEW product_variation_stock_view AS
SELECT
    product_variations.product_id AS product_id,
    product_variations.id AS product_variation_id,
    COALESCE(SUM(stocks.quantity) - COALESCE(SUM(product_variation_order.quantity), 0), 0) AS stock,
    CASE WHEN COALESCE(SUM(stocks.quantity) - COALESCE(SUM(product_variation_order.quantity), 0), 0) &gt; 0
        THEN true
        ELSE false
    END in_stock
GROUP BY product_variations.id, product_variations.product_id
FROM product_variations