简体   繁体   中英

How to find the repeat customers in the list?

I Have Purchase Table Containing 5 Columns

Columns Names Are

CustomerID, BillID, ProductID, unatity, Payment_Type

Columns Values Are

CID00001, BID00001, PID001, 1, Card

Total Customers Count - 37156

DISTINCT Customers Count - 26053

How to Find the repeat Customers? (37156 - 26053 = 11103)

Aggregation is one way:

SELECT COUNT(*) AS num_repeat
FROM
(
    SELECT CustomerID
    FROM purchases
    GROUP BY CustomerID
    HAVING COUNT(*) > 1
) t;

To get the list of repeat customers,

SELECT CustomerID, COUNT(*) AS PurchaseCount
FROM purchases
GROUP BY CustomerID
HAVING COUNT(*) > 1

You can use this:

SELECT * FROM Purchase 
WHERE CustomerID 
IN(
    SELECT CustomerID FROM Purchase
    GROUP BY CustomerID HAVING COUNT(*) > 1
    )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM