0

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

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

数据应该是什么样子

我也创建了一个示例数据库。我还试图通过新客户和回头客以及后来的年龄范围来打破客户。

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 购物的新老客户。请帮忙 !

4

1 回答 1

1

你快到了。尝试这个:

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
于 2019-03-12T11:55:34.063 回答