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;
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
i have 3 table,
- Employee (idemployee, iddivision, firstname, lastname)
- Salary (idsalary, idemployee, dateadded, amount)
- division (iddivision, divisionname)
i want to display the first name that have the highest amount between january and april
so far i tried
SELECT firstname, MAX(Total) FROM (
SELECT firstname,SUM(amount) AS Total
FROM salary
JOIN employee ON employee.idemployee=salary.idemployee
WHERE dateadded BETWEEN "2019-1-1" AND "2019-4-1"
GROUP BY employee.idemployee) as t
but the employeeid sql show is wrong. why?
SELECT employee.firstname, SUM(salary.amount) AS Total
FROM salary
JOIN employee
ON employee.idemployee=salary.idemployee
WHERE salary.dateadded BETWEEN "2019-01-01" AND "2019-04-01"
GROUP BY employee.firstname
ORDER BY 2 DESC
LIMIT 1
You are filtering by the range you want, then you sum the amounts in that range, grouping by the employee. If you order by that sum and get just the first row, it must he one what you are looking for.
Even better, if your employees just have a name in the firstname attribute, you have the risk to group by the same name wrongly. So, to identify better the employee, I would add the idemployee to the group by sentence. Like this:
SELECT employee.idemployee, employee.firstname, SUM(salary.amount) AS Total
FROM salary
JOIN employee
ON employee.idemployee=salary.idemployee
WHERE salary.dateadded BETWEEN "2019-01-01" AND "2019-04-01"
GROUP BY employee.idemployee,employee.firstname
ORDER BY 3 DESC
LIMIT 1
Do you mean you want it to be ordered from greatest to least?
SELECT firstname, Total FROM (
SELECT firstname,SUM(amount) AS Total
FROM salary
JOIN employee ON employee.idemployee=salary.idemployee
WHERE dateadded BETWEEN "2019-1-1" AND "2019-4-1"
GROUP BY employee.idemployee) as t
order by desc Total
I have the following ORDERS table
I know query to select the customer that has ordered the greatest quantity. However, how would it work, if say, two customers have the same quantity. What query should I write to show both the customers?
You can use a subquery which checks that the quantity for a given record matches the largest quantity observed in the table:
SELECT *
FROM yourTable
WHERE qty = (SELECT MAX(qty) FROM yourTable)
This will return multiple records if there are more than one customer sharing the maximum quantity.
If you only wanted to get back a single record, even in the presence of ties, you could use this approach:
SELECT *
FROM yourTable
ORDER BY qty DESC
LIMIT 1
I think you want sum of qty per custNum.
If so you can try like:
select custNum,
sum(qty) as qty
from Orders
group by custNum
order by sum(qty) desc;
Fiddle here:
http://sqlfiddle.com/#!9/47931b/10
SELECT custnum,sum(qty) as total
FROM orders
group by custnum
having sum(qty) = (SELECT MAX(qty) FROM orders);
This will return both values.
I'm trying to make a query with max and count like this one: (taken from http://www.w3resource.com/sql/aggregate-functions/max-count.php)
SELECT MAX (mycount)
FROM (SELECT agent_code,COUNT(agent_code) mycount
FROM orders
GROUP BY agent_code);
this query returns a column with the name 'MAX(MYCOUNT)' with the max value: '7',the simple change I want is that I would like to get the agent code of the one who got the maximum, instead of the max records of agent code.
tried to do this in some ways but no luck so far,
hope you can help me to do this right.
If you don't have to worry about a tie for two agents having the max number of orders, then you can try the following:
SELECT agent_code, COUNT(*) AS mycount
FROM orders
GROUP BY agent_code
ORDER BY COUNT(*) DESC
LIMIT 1
If you do have to worry about a tie for the max number of orders, and you want all ties, then you can use a subquery:
SELECT agent_code, COUNT(*) AS mycount
FROM orders
GROUP BY agent_code
HAVING COUNT(*) = (SELECT MAX(t.mycount) FROM
(SELECT COUNT(*) AS mycount FROM orders GROUP BY agent_code) t)
You can use order by and limit:
SELECT agent_code, COUNT(agent_code) as mycount
FROM orders
GROUP BY agent_code
ORDER BY mycount DESC
LIMIT 1;
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