如何在 SQL 上左连接两个查询?

How to left join two queries on SQL?

我正在编写代码,但它不接受我的加入。我希望我的现有查询按同月和同年添加来自另一个查询组的总用户结果。在我尝试添加左连接之前,代码一直有效。

SELECT first_year, first_month, product_line, Users,
     SUM(CASE WHEN month_number = 0 THEN 1 ELSE 0 END) AS month_0,
       SUM(CASE WHEN month_number = 1 THEN 1 ELSE 0 END) AS month_1,
       SUM(CASE WHEN month_number = 2 THEN 1 ELSE 0 END) AS month_2,
       SUM(CASE WHEN month_number = 3 THEN 1 ELSE 0 END) AS month_3,
       SUM(CASE WHEN month_number = 4 THEN 1 ELSE 0 END) AS month_4,
       SUM(CASE WHEN month_number = 5 THEN 1 ELSE 0 END) AS month_5,
       SUM(CASE WHEN month_number = 6 THEN 1 ELSE 0 END) AS month_6,
       SUM(CASE WHEN month_number = 7 THEN 1 ELSE 0 END) AS month_7,
       SUM(CASE WHEN month_number = 8 THEN 1 ELSE 0 END) AS month_8

FROM (SELECT a.purchase_month, a.purchase_year,b.first_month,b.first_year, a.purchase_month-b.first_month as month_number, a.product_line
FROM (SELECT user_id, month(purchase_date) AS purchase_month, year(purchase_date) AS purchase_year, product_line
        FROM purchase) a,(SELECT user_id, month(first_purchase_date) AS first_month, year(first_purchase_date) as first_year, product_line 
                                        FROM first_purchase
                                        WHERE product_line="Restaurant") b
WHERE a.user_id=b.user_id AND purchase_year=first_year
HAVING a.product_line="Restaurant") AS with_month_number
LEFT JOIN (SELECT COUNT(user_id) AS first_users, year(first_purchase_date) as first_year, month(first_purchase_date) AS first_month, product_line
            FROM first_purchase
            WHERE product_line="Restaurant"
            GROUP BY year(first_purchase_date), month(first_purchase_date), product_line) AS j
ON first_year=j-year(first_purchase_date) AND first_month=j.month(first_purchase_date)
GROUP BY first_year, first_month, product_line
ORDER BY first_year, first_month;

良好的缩进可以显着帮助了解什么与什么相关联。我在诸如没有任何分组依据的 HAVING 子句问题、用于连接的歧义列和最终分组依据等问题中发表了评论,因为两个表具有相同的列名。 SQL 需要知道哪个。如果有疑问并且您有多个表,请始终在查询中使用 table.column 或 alias.column。

SELECT 
        first_year, 
        first_month, 
        product_line, 
        Users,
        SUM(CASE WHEN month_number = 0 THEN 1 ELSE 0 END) AS month_0,
        SUM(CASE WHEN month_number = 1 THEN 1 ELSE 0 END) AS month_1,
        SUM(CASE WHEN month_number = 2 THEN 1 ELSE 0 END) AS month_2,
        SUM(CASE WHEN month_number = 3 THEN 1 ELSE 0 END) AS month_3,
        SUM(CASE WHEN month_number = 4 THEN 1 ELSE 0 END) AS month_4,
        SUM(CASE WHEN month_number = 5 THEN 1 ELSE 0 END) AS month_5,
        SUM(CASE WHEN month_number = 6 THEN 1 ELSE 0 END) AS month_6,
        SUM(CASE WHEN month_number = 7 THEN 1 ELSE 0 END) AS month_7,
        SUM(CASE WHEN month_number = 8 THEN 1 ELSE 0 END) AS month_8
    FROM 
        (   SELECT 
                    a.purchase_month, 
                    a.purchase_year,
                    b.first_month,
                    b.first_year, 
                    a.purchase_month - b.first_month as month_number, 
                    a.product_line
                FROM 
                    (   SELECT 
                                user_id, 
                                month(purchase_date) AS purchase_month, 
                                year(purchase_date) AS purchase_year, 
                                product_line
                            FROM 
                                purchase 
                                -- you had HAVING her, but no GROUP BY, so just pulled into WHERE clause
                            WHERE
                                product_line = 'Restaurant' ) a,
                    (   SELECT
                                user_id, 
                                month(first_purchase_date) AS first_month, 
                                year(first_purchase_date) as first_year, 
                                product_line 
                            FROM 
                                first_purchase
                            WHERE 
                                product_line = 'Restaurant' ) b
                WHERE 
                        a.user_id = b.user_id 
                    AND a.purchase_year = b.first_year ) AS with_month_number

            LEFT JOIN 
            (   SELECT 
                        COUNT(user_id) AS first_users, 
                        year(first_purchase_date) as first_year, 
                        month(first_purchase_date) AS first_month, 
                        product_line
                    FROM 
                        first_purchase
                    WHERE 
                        product_line = 'Restaurant'
                    GROUP BY 
                        year(first_purchase_date), 
                        month(first_purchase_date), 
                        product_line ) AS j
                -- ambiguity here on JOIN...
                -- should it be with_month_number.first_year and with_month_number.first_month
                --ON first_year = j - year(first_purchase_date) 
                --AND first_month = j.month(first_purchase_date)

                -- changed to
                ON with_month_number.first_year = j.first_year 
                AND with_month_number.first_month = j.first_month
    GROUP BY 
        with_month_number.first_year, 
        with_month_number.first_month, 
        with_month_number.product_line
    ORDER BY 
        with_month_number.first_year, 
        with_month_number.first_month;