🌟 explained PostgreSQL solution 🚀 Beats 85% and use only one INNER JOIN and one DISTINCT
Explaination\n\n- Common Table Expression (CTE) - product_id_with_first_year:\n        SELECT DISTINCT ON(product_id) product_id, year FROM Sales ORDER BY produ
# Explaination\n\n- **Common Table Expression (CTE) - product_id_with_first_year**:\n        `SELECT DISTINCT ON(product_id) product_id, year FROM Sales ORDER BY product_id, year ASC`: This CTE is used to select the earliest sales record for each product_id based on the year. The DISTINCT ON clause ensures that for each product_id, only the first entry (the earliest year) is selected.\n\n- **Main Query**:\n    - The main query then joins the CTE with the original Sales table to get the detailed sales records for the first year of sales for each product.\n    - `INNER JOIN Sales USING (product_id, year)`: This join is made on both product_id and year to ensure that the sales data corresponds to the first year of sales for each product as identified in the CTE.\n\n\n# Code\n```\nWITH product_id_with_first_year AS (\n    SELECT DISTINCT ON(product_id)\n        product_id, year\n    FROM Sales\n    ORDER BY product_id, year ASC\n)\n\nSELECT product_id,\n        year AS first_year,\n        quantity,\n        price \nFROM product_id_with_first_year\nINNER JOIN Sales\nUSING (product_id, year);\n```\n____\n\nIf it was useful don\'t hesitate to upvote \uD83C\uDF20 Or keep it in your favorite \uD83C\uDF1F\n![Upvote invitation](https://github.com/MathieuSoysal/CROUS-assistant-Collector/assets/43273304/b801cf95-dcda-4733-ada2-939f5fcb29e0)\n
['PostgreSQL']