SQL - 尝试通过购物渠道寻找客户

SQL - Trying to find customer by their shopping channel

您好,我正在尝试寻找只在网上购物和在实体店购物的顾客以及同时在网上和实体店购物的顾客。所以当我把它们加起来时,它们应该等于我的总客户数。

我正在尝试通过他们的购物渠道找到新客户和回头客。我需要一个 sql 来为我提供所有在商店购物的新客户和回头客,然后在一个单独的 table 中为所有只在网上购物的 new/returning 客户,然后是人们在网上和实体店都购物过的人(跨界顾客)。因此,当我将它们加在一起时,它们应该等于我在每个类别中的总客户数(新客户和回头客)。 它应该如下所示:

how data should look like

我也创建了一个示例数据库。我也在尝试通过新客户和回头客以及他们的年龄段来打破客户。

https://dbfiddle.uk/?rdbms=oracle_11.2&fiddle=96a7b85c8ca0da7f7c40f20205964d9b

这些是我尝试过的一些查询:下面是向我展示了只在网上购买过的新客户和回头客的查询:

SELECT
    DECODE(is_new, 1, 'New Customers', 'Returning Customers') type_of_customer,
    COUNT(distinct individual_id) count_of_customers,
    SUM(count_of_transactions) count_of_transactions,
    SUM(sum_of_quantity) sum_of_quantity
FROM (
    SELECT
    individual_id,
    SUM(dollar_value_us),
    sum(quantity) sum_of_quantity,
    count(distinct transaction_number) count_of_transactions,
    CASE WHEN MIN(txn_date) = min_txn_date THEN 1 ELSE 0 END is_new
FROM (
    SELECT 
        a.individual_id, 
        a.dollar_value_us,
        a.txn_date,
        a.quantity,
        a.transaction_number,
        b.gender,
        b.age,
        MIN(a.txn_date) OVER(PARTITION BY a.individual_id) min_txn_date,
        A.TRANTYPE

    FROM transaction_detail_mv   a
    join gender_details b on a.individual_id = b.individual_id
    WHERE 
        a.brand_org_code = 'BRAND'
        AND a.is_merch = 1
        AND a.currency_code = 'USD'
        AND a.line_item_amt_type_cd = 'S'
        AND a.individual_id not in (select individual_id from transaction_detail_mv where trantype = 'POS' )

)
WHERE 
    txn_date >= TO_DATE('10-02-2019', 'DD-MM-YYYY') 
    AND txn_date < TO_DATE('17-02-2019', 'DD-MM-YYYY')
GROUP BY
    individual_id,
    min_txn_date
    )
GROUP BY is_new

寻找通过 POS 购买的新老客户如下:

SELECT
        DECODE(is_new, 1, 'New Customers', 'Returning Customers') type_of_customer,
        COUNT(distinct individual_id) count_of_customers,
        SUM(count_of_transactions) count_of_transactions,
        SUM(sum_of_quantity) sum_of_quantity
    FROM (
        SELECT
        individual_id,
        SUM(dollar_value_us),
        sum(quantity) sum_of_quantity,
        count(distinct transaction_number) count_of_transactions,
        CASE WHEN MIN(txn_date) = min_txn_date THEN 1 ELSE 0 END is_new
    FROM (
        SELECT 
            a.individual_id, 
            a.dollar_value_us,
            a.txn_date,
            a.quantity,
            a.transaction_number,
            b.gender,
            b.age,
            MIN(a.txn_date) OVER(PARTITION BY a.individual_id) min_txn_date,
            A.TRANTYPE

        FROM transaction_detail_mv   a
        join gender_details b on a.individual_id = b.individual_id
        WHERE 
            a.brand_org_code = 'BRAND'
            AND a.is_merch = 1
            AND a.currency_code = 'USD'
            AND a.line_item_amt_type_cd = 'S'
            AND a.individual_id not in (select individual_id from transaction_detail_mv where trantype = 'ONLINE' )

    )
    WHERE 
        txn_date >= TO_DATE('10-02-2019', 'DD-MM-YYYY') 
        AND txn_date < TO_DATE('17-02-2019', 'DD-MM-YYYY')
    GROUP BY
        individual_id,
        min_txn_date
        )
    GROUP BY is_new

我正在尝试寻找在线和 POS 购物的新老客户。请帮忙!

你快到了。试试这个:

SELECT
        DECODE(is_new, 1, 'New Customers', 'Returning Customers') type_of_customer,
        COUNT(distinct individual_id) count_of_customers,
        SUM(count_of_transactions) count_of_transactions,
        SUM(sum_of_quantity) sum_of_quantity
    FROM (
        SELECT
        individual_id,
        SUM(dollar_value_us),
        sum(quantity) sum_of_quantity,
        count(distinct transaction_number) count_of_transactions,
        CASE WHEN MIN(txn_date) = min_txn_date THEN 1 ELSE 0 END is_new
    FROM (
        SELECT 
            a.individual_id, 
            a.dollar_value_us,
            a.txn_date,
            a.quantity,
            a.transaction_number,
            b.gender,
            b.age,
            MIN(a.txn_date) OVER(PARTITION BY a.individual_id) min_txn_date,
            A.TRANTYPE

        FROM transaction_detail_mv   a
        join gender_details b on a.individual_id = b.individual_id
        WHERE 
            a.brand_org_code = 'BRAND'
            AND a.is_merch = 1
            AND a.currency_code = 'USD'
            AND a.line_item_amt_type_cd = 'S'
            AND a.individual_id not in (select individual_id from transaction_detail_mv where ((trantype = 'ONLINE') OR (trantype = 'POS') )

    )
    WHERE 
        txn_date >= TO_DATE('10-02-2019', 'DD-MM-YYYY') 
        AND txn_date < TO_DATE('17-02-2019', 'DD-MM-YYYY')
    GROUP BY
        individual_id,
        min_txn_date
        )
    GROUP BY is_new