count multiple attributes sql - mysql

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

Related

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
)

How to solve this MySQL COUNT query?

Three table are given:
customer(cust_id, name, address, sales_id)
orders(order_id, cust_id,date, sales_id)
salesman(sales_id,commision)
and you have to write an MySQL query to "count the salesman by their order_id and date". Is the question is correct? if yes, how can I solve this.
The question is not clear, but the codes below will give you an idea of how to get aggregated results and you can play around / adjust based on your needs
select
date,
sales_id,
count(order_id) as total_orders_per_date_and_sales_id
from orders
group by 1,2
if you need the total salesman per date then you can do this:
select
date,
count(sales_id) as total_sales_man_per_date
from orders
group by 1
if one order has multiple sales owners, then you can find total salesman per order id
select
orders_id,
count(salesman) as total_sales_man_involved_per_order
from orders
group by 1
if you need total commission per salesman per date:
select
orders.date,
orders.sales_id,
sum(salesman.comission) as total_comssion_per_salesman_per_date
from orders
left join salesman
on orders.sales_id = salesman.sales_id
group by 1,2
total number of distinct salesman
select
count(distinct sales_id) as total_unique_salesman
from salesman

How to display only value that occurs second highest number of times?

Write a query to display the customer name who visited the second highest number of times
select customer_id,count(*) from booking group by customer_id ;
using this query i got the count of number of visits for each customer as shown below
CUSTOMER_ID,COUNT(*)
C001,6
C002,1
C003,1
C004,1
C005,4
but i want to display only c005 since he has visited the second maximum time
SELECT customer_id, COUNT(*)
FROM booking
GROUP BY customer_id
HAVING COUNT(*) <> (SELECT MAX(t.custCount)
FROM (SELECT COUNT(*) AS custCount
FROM booking
GROUP BY customer_id) t )
ORDER BY COUNT(*) DESC
LIMIT 1
As a side note, this won't work if there are ties for second place. In this case, you use the above query as a condition in the WHERE clause, e.g.
SELECT customer_id
FROM booking
GROUP BY customer_id
HAVING COUNT(*) = (query given above)
You can use a outer query and filter the same like
select customer_id from (
select customer_id,
count(*) as datacount
from booking
group by customer_id ) xxx
order by datacount desc
limit 1;

Use sql to calculate a market penetration ratio?

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

SQL Query of Using Functions - How to get MAXIMUM Count of the list

How can I query for the MAXIMUM COUNT Number of transaction...
My code is as follows:
SELECT customer_id, COUNT(customer_id)
FROM rental
GROUP BY customer_id
HAVING MAX(COUNT(customer_id)); //I need to get the MAXIMUM COUNT of the list
If you are looking for the customer_id with largest number of rows in the rental table, you could use:
SELECT customer_id, COUNT(customer_id) as CustomerRowCount
FROM rental
GROUP BY customer_id
ORDER BY COUNT(customer_id) DESC
LIMIT 1
The above query will count all of the records for each customer_id, sort them in descending order, and then select only the top row. The maximum count for a customer will be present in CustomerRowCount.
EDIT
Conrad brought up the point that two or more customer_id's may have the same number of records. If the business requirement is that multiple records should be returned in this case, then his query will give you the result you're looking for. If only one record should be returned, then a simple, consistent way to break ties would just be to take the user with the lowest customer_id. To do that, you can just change the ORDER BY statement to:
ORDER BY COUNT(customer_id) DESC, customer_id
Try:
SELECT customer_id, MAX(COUNT(customer_id))
FROM rental
GROUP BY customer_id;
or
SELECT top 1 customer_id, COUNT(customer_id)
FROM rental
GROUP BY customer_id
ORDER BY COUNT(customer_id) DESC
Since more than one customer can have the same Maximum of the count you should do as follows
SELECT customer_id,
COUNT(customer_id) AS customerrowcount
FROM rental
GROUP BY customer_id
HAVING COUNT(customer_id) = (SELECT COUNT(customer_id)
FROM rental
GROUP BY customer_id
ORDER BY COUNT(customer_id) DESC
LIMIT 1)
However if you're ok with an arbitrary customer being selected than you should use rsbarro's answer
Query for MySQL
SELECT
*
FROM (SELECT
customer_id,
COUNT(customer_id) AS CountOfCustomer
FROM rental
GROUP BY customer_id) q1
ORDER BY CountOfCustomer DESC
LIMIT 1