您好,我试图找到仅在线购物和仅在商店购物的客户以及同时在线和在商店购物的客户。因此,当我将它们加起来时,它们应该等于我的总客户数。
我正试图通过他们的购物渠道找到新的和回头客。我需要一个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购物的新老客户。请帮助!
答案 0 :(得分: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