MySQL GROUP BY and HAVING Clause - mysql

I am trying to write a script to show the names of every city with at least two customers, along with the number of customers in that city.
Here's what I have but I cannot figure out how to get the number of customers.
Am I close?
SELECT CONCAT (FName,' ',LName) AS Customers, city
FROM Customer
GROUP BY City
HAVING COUNT(CID) >= 2

Use count(*)
SELECT city , count(*)
FROM Customer
GROUP BY City HAVING COUNT(*) >= 2

As u can see from this example:
SELECT Employees.LastName, COUNT(Orders.OrderID) AS NumberOfOrders FROM (Orders
INNER JOIN Employees
ON Orders.EmployeeID=Employees.EmployeeID)
GROUP BY LastName
HAVING COUNT(Orders.OrderID) > 10;
you must enter COUNT (CID) in the select

Related

SQL Query, number of city and continent

I have a problem with a query.
I have to find for all the continent: the name of the continent, number of cities and number of countries. This is what I did
SELECT co.continent, COUNT(*)
FROM Country co
JOIN City c ON c.countrycode = co.code
GROUP BY co.continent
UNION
SELECT COUNT(*)
FROM Country co2
WHERE co.continent = co2.continent ( <---- ??? )
GROUP BY co2.continent
But I don't know if is it legal the part "WHERE co.continent = co2.continent" because the second query isn't a subquery of the first, is it? Is there another way to do this query?
UNION is not required, a single query with GROUP BY and COUNT aggregate will get the desired result, there could be multiple cities in the same country, a country could appear multiple times, use COUNT(DISTINCT...) to remove duplicates.
SELECT co.continent, COUNT(*) cities, COUNT(DISTINCT co.code) countries
FROM Country co
JOIN City c ON c.countrycode = co.code
GROUP BY co.continent
co.continent = co2.continent in the original union query is invalid. Queries in union are independent from each other.

MYSQL getting total of summed column

have this problem for a school example problem where I have to get the total salary for coaches and participants in March (done below) and then I have to sum to get the total salary due in March for all employees which I just want to add onto the end of the Total Salary column.
This is what I have so far:
(SELECT Coach.name AS Name, COUNT(*) AS 'Shows Attended In March',
dailySalary AS 'Daily Salary', sum(dailySalary) AS 'Total Salary'
FROM Coach, TVShow, CoachInShow
WHERE monthname(dateOfShow)='March' AND
Coach.idCoach=CoachInShow.idCoach AND TVShow.idShow =
CoachInShow.idShow
GROUP BY Coach.name, Coach.dailySalary)
UNION
(SELECT Participant.name AS Name, COUNT(*) AS 'Shows Attended In
March', dailySalary AS 'Daily Salary', sum(dailySalary) AS 'Total
Salary'
FROM Participant, TVShow, Contender, ContenderInShow
WHERE monthname(dateOfShow)='March' AND Participant.idContender =
Contender.idContender AND Contender.idContender =
ContenderInShow.idContender AND ContenderInShow.idShow = TVShow.idShow
GROUP BY Participant.name, Participant.dailySalary);
I tried using GROUP BY WITH ROLLBACK on the whole thing but it doesn't add up only the TotalSalary columns. I've spent a while on this and kinda stumped.
I pasted the data here for what I'm working with: https://www.db-fiddle.com/f/gPKVQrZCMkvHUqViAUzCqZ/0 http://sqlfiddle.com/#!9/535f6d/1
Put the UNION into a subquery. In the main query, sum all the counts and total salaries, and use WITH ROLLUP to get the grand total.
You don't need dailySalary in the GROUP BY clause, since it's functionally dependent on the ID.
SELECT name AS Name, SUM(count) AS `Shows Attended in March`, SUM(totalSalary) AS `Total Salary`
FROM (
SELECT Coach.name, COUNT(*) AS count, SUM(dailySalary) AS totalSalary
FROM Coach
JOIN CoachInShow ON Coach.idCoach=CoachInShow.idCoach
JOIN TVShow ON TVShow.idShow = CoachInShow.idShow
WHERE monthname(dateOfShow)='March'
GROUP BY Coach.idCoach
UNION
SELECT Participant.name, COUNT(*) AS count, SUM(dailySalary) AS totalSalary
FROM Participant
JOIN Contender ON Participant.idContender = Contender.idContender
JOIN ContenderInShow ON Contender.idContender = ContenderInShow.idContender
JOIN TVShow ON ContenderInShow.idShow = TVShow.idShow
WHERE monthname(dateOfShow)='March'
GROUP BY Participant.idParticipant
) AS x
GROUP BY Name
WITH ROLLUP
DEMO

how to display highest order count i,e is in table(tcount) with customer name( name)

I created 2 tables: customers (table1) and orderss (table2)
In table1 I created columns(id,name) and in table2 I created columns(oid,customer_id,amount).
Next I used the sum() function to get the total amount of particular customers and I used count() to get the count of total orders ordered by particular customer and I displayed respectively using group by.
Now I do not know how to find the highest orders with particular customer name.
This is what I tried:
select max(name) as aaa,max(tcount) as highorders
from
( SELECT name,sum(orderss.amount) AS tamount,count(orderss.oid) as tcount
FROM customers
INNER JOIN orderss ON customers.id=orderss.customer_id group by name
having tamount > 50 and tcount >= 2
) as ho
use order by
select max(name) as aaa,max(tcount) as highorders
from
( SELECT name,sum(orderss.amount) AS tamount,count(orderss.oid) as tcount
FROM customers
INNER JOIN orderss ON customers.id=orderss.customer_id group by name
having tamount > 50 and tcount >= 2
) as ho
order by field_name DESC
(you can get an ascending order by replacing DESC with ASC

SQL query to compute total number of customers who purchase >4 products on one day

I want to write a SQL query to compute which customers have purchased more than 4 products on the same day.
Here are my tables:
Sales
(date, customer_id, product_id, units_sold)
Products
(id, name, price)
Customers
(id, name)
& here's what I have so far:
SELECT COUNT(s.product_id) as total_customers
FROM Sales s1
WHERE DATEDIFF(s1.date, s2.date)=0
INNER JOIN Sales s2
ON s1.product_id = s2.product_id
HAVING COUNT(s.product_id) > 4;
If you want the customers who have purchased more than 4 products on the same date:
SELECT DISTINCT s.customer_id
FROM Sales s
GROUP BY s.customer_id, date(s.date)
HAVING COUNT(*) > 4;
This is one of the few cases where SELECT DISTINCT is used with GROUP BY. If you want to know the dates as well, then include date(s.date) in the SELECT.
Note that this assumes that any given product is purchased by a customer only once on each date. If a customer can have multiple records for a single product on one date, use COUNT(DISTINCT product_id) instead of COUNT(*).
To get the total number of customers, use a subquery:
SELECT COUNT(*)
FROM (SELECT DISTINCT s.customer_id
FROM Sales s
GROUP BY s.customer_id, date(s.date)
HAVING COUNT(*) > 4
) c
Too many mistakes's in your query try this
SELECT customer_id,cast(s1.date as date),COUNT(s1.product_id) as total_customers
FROM Sales s1
Group by customer_id,cast(s1.date as date)
HAVING COUNT(s1.product_id) > 4;

A cleaner way to do this SQL query

So have a list of customers, that have a list of customers.
I have 2 tables, customers and invoices.
Customer_id can be the same for my customers e.g
Client A
Customer #1
Client B
Customer #1
So invoice table can have customer_id of 1, for different customers.
The below query works, but seems a little messy. Any ideas to clean it up?
SELECT name,
sum(sub_total) total
FROM customers, invoices
WHERE customer_id = 1438 and
invoices.customer_id = 1438 and
customers.customer_id = invoices.customer_id
GROUP BY name
Order By total DESC;
If A = B and A = 1438 then B = 1438 you don't need to check it...
(Trust me on this one, I got 60+ in math in highschool)
SELECT name, sum(sub_total) total
FROM customers, invoices
WHERE invoices.customer_id = 1438
AND customers.customer_id = invoices.customer_id
GROUP BY name
ORDER BY total DESC;
Or with explicitly saying which type of JOIN you want:
SELECT name, sum(sub_total) total
FROM customers INNER JOIN invoices
ON customers.customer_id = invoices.customer_id
WHERE invoices.customer_id = 1438
GROUP BY name
ORDER BY total DESC;
you dont need the extra check...
SELECT name, sum(sub_total) total
FROM customers, invoices
WHERE
customer_id = 1438 and
customers.customer_id = invoices.customer_id
GROUP BY name order by total DESC;
I would strongly NOT do a group by on name... Say you have two customers named "Bill Smith", but one is customer ID 10, and the other is customer ID 785... You just combined them into a single person. You SHOULD be grouping by ID, not the name...
Now, that said, you are querying for only a single customer ID anyhow and will only ever return a single record. If what you are really trying to accomplish is getting ALL customers and the tot of their respective invoices, remove the where clause of the single customer. You can keep the group by ID, but show the actual customer's name. If you have multiple customers with the same total, you can sub-sort by their name, but the grouping is STILL based on just the customer's ID.
SELECT
c.name,
sum(i.sub_total) total
FROM
customers
JOIN invoices
on c.Customer_ID = i.Customer_ID
GROUP BY
c.customer_ID
Order By
total DESC,
c.Name