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
)
Related
I'm having trouble coming up with a query to get the number of customers who purchased on multiple dates.
We're given a table of product purchases. Each row in the table represents an individual user product purchase.if the customer purchased two things on the same day that does not count as an upsell as they were purchased within a similar timeframe.
'transactions' table:
column
type
id
integer
user_id
integer
created_at
datetime
product_id
integer
quantity
integer
I tried in this way
select count(*)
from
( select user_id
, count(date)
from
( SELECT user_id
, DATE(created_at) AS date
FROM transactions
GROUP BY 1,2
) S
group
by 1
having count(date)>1
) A
I think you want:
SELECT COUNT(*)
FROM
(
SELECT user_id
FROM transactions
GROUP BY user_id
HAVING COUNT(DISTINCT DATE(created_at)) > 1
) t;
The subquery finds all users having transacted on more than one date, the outer query finds the count of such users.
Count the distinct dates per user, first, then count from table when the count is > 1.
See below:
WITH T as (select user_id,
count(distinct DATE(created_at)) as count
from transactions
GROUP BY user_id)
select count(*) from T where count > 1
Given a payments table that includes payer_id and recipient_id
And calculating received payments would be:
select COUNT(*) as payer_count, recipient_id
FROM payments
GROUP BY recipient_id
ORDER BY payer_count DESC
how do you calculate: the number of payments that a user is on(a user can be either a payer_id or a recipient_id)?
If is suppose that you want to count - for every person with a particular id - the number of payment transactions in which this person has been involved, then a union all should help:
select person_id, count(*)
from ((select payer_id as person_id from payments) union all (select recipient_id as person_id from payments)) paymentsOfPerson
group by person_id
order by person_id DESC
If payments can also contain records where payer_id = recipient_id (transfer from one account to another account of the same person), then one has to take care that such transfers are not counted twice:
select person_id, count(*)
from ((select payer_id as person_id from payments where payer_id != recepient_id) union all (select recipient_id as person_id from payments)) paymentsOfPerson
group by person_id
order by person_id DESC
One method is UNION ALL before the aggregation:
select id, count(*) as cnt, sum(recipient) as recipient_cnt,
sum(payer) as payer_cnt
from ((select recipient_id as id, 1 as recipient, 0 as payer
from payments
) union all
(select payer_id, 0, 1
from payments
)
) i
group by id;
I think this can solve your problem:
SELECT COUNT(*) as payments_count
, COUNT(DISTINCT payer_id) distinct_payers
, recipient_id
FROM payments
GROUP BY recipient_id
ORDER BY payer_count DESC
I have a Mysql Query where
Where i have a table as like below(Sample data)
Employee_id Months Salary
1 10 200
2 20 300
3 30 400
Now i wanted to find the Number of Employees who are having the Maximum total salary
(Total salary =month * salary)
So i have my query like this
Subquery:
((select max(mon_sal.mc) as max_mc from (
select months*salary as mc from employee group by employee_id) as mon_sal)
as max_mon_sal)
//To find the Maximum of Total salary
Now my problem is i need to find the number of persons having the maximum salary,
select max_mon_sal.max_mc,name
from employee group by employee_id
having salary=max_mon_sal.max_mc from (
(select max(mon_sal.mc) as max_mc from
(select months*salary as mc from employee group by employee_id) as mon_sal)
as max_mon_sal)
Its showing Error.I have problem with using the max_mon_sal alias.Please suggest.
You can simply use:
select count(*)
from employee
where months * salary = (
select max(months * salary)
from employee
);
SELECT COUNT(*) no_of_employees
FROM my_table a
JOIN
( SELECT MAX(months*salary) total FROM my_table ) b
ON b.total = a.months * a.salary;
I don't no sure if this is what are looking for, even the better solution for this case is to use temporary tables:
SELECT Employee.employee_id,SUM(MONTHs * salary)
FROM Employee,
(
SELECT MAX(total) value FROM (
SELECT SUM(MONTHs * salary) as total
FROM Employee
GROUP BY employee_id
) T
) D
GROUP BY Employee.employee_id,D.value
HAVING SUM(MONTHs * salary) = D.value
I have a table 'transactions' of purchasing activity:
customer_id, company, purchaseamount
and am looking for an sql command to output the "market penetration ratio" for a given company:
For example, I want to calculate:
(number of unique customer_id who bought from company == 12) / (total number of unique customer_id)
select count(distinct customer_id) * 100 / (select count(distinct customer_id) from transactions)
from transactions
where customer_id = 12
You can do this using conditional aggregation:
select count(distinct case when company = 12 then customer_id end) / count(distinct customer_id)
from transactions;
The unique number of customer_ids is a single row, so you can just cross join it with the distinct count per company
SELECT company, COUNT (DISTINCT customer_id) / cnt AS penetration_ratio
FROM my_table
CROSS JOIN (SELECT COUNT (DISTINCT customer_id) AS cnt
FROM my_table) t
i am looking for the following output
10 customers with 10 items bought in 2 product groups
8 customers with 10 items bought in 1 product group.
Generally a matrix with a customer count over items bought AND product groups, ie. counting over 2 attributes (items and product group)
I tried the code below, but then it only gives me
1 customer with 10 items and 2 product groups
1 customer with 10 items and 1 product group despite the fact that there is many more customers in each line:
snippet,
count (distinct customer_id) over (Partition by customer_id) as Customer_ID
,count (distinct customer_shipment_item_id) Over (Partition by customer_id) as customer_items
,count (distinct product_group) Over (Partition by customer_id) as customer_product_groups
Can you explain how this would work?
Try this. Unless I don't understand what you are looking for, you don't need the Partition By clause.
count (distinct customer_id) as Customer_ID
,count (distinct customer_shipment_item_id) as customer_items
,count (distinct product_group) as customer_product_groups
I think something like this would give you what you are looking for:
SELECT COUNT(customer_id) Customer_ID, customer_items, customer_product_groups
FROM
(SELECT customer_id, COUNT(DISTINCT customer_shipment_item_id) as customer_items,
COUNT(DISTINCT product_group) as customer_product_groups
FROM TABLENAME
JOIN <YOUR JOINS>
WHERE customer_id IN (SELECT customer_ID FROM <YOUR JOINS> WHERE customer_shipment_item_id = x)
GROUP BY customer_id
) a
GROUP BY customer_items, customer_product_groups