How to sum total bookings for a particular flight - mysql

Ok so i have two columns in a table. One of these says how many people the customer booked to go to a particular country.
The other column shows which country the order was booked for, for example, fiji, australia.
Im having trouble adding up the No_Of_People column in order to determine which flight destination has the most people going to it.
If there was only one booking per country this would be easy, as it would be something like:
select destination_name from bookings order by sum(No_Of_People) desc limit 1;
Just wondering how i can do this, knowing that there are multiple people booking for the same country.

You need to group by destination to get the people per destination:
SELECT
destination_name,
sum(No_Of_People) AS count
FROM
bookings
GROUP BY destination_name
ORDER BY count desc;
https://dev.mysql.com/doc/refman/5.7/en/group-by-handling.html

select sum(No_Of_People), destination_name from bookings group by destination_name order by sum(No_Of_People) desc

Related

Group how many times a Sales Rep worked with a customer Descending

Display a count of customers each sales rep has worked with. Sort by the count in descending order.
Rep_Name
Cust_Name
I have tried many ways to write this out but just not getting the results I am looking for...
Select Rep_Name From finaldb.rep
count(customer) Desc

How to retrieve the salesmans that has sold an item to every client (another column)

first time here. I'm a beginner
IM USING MYSQL.
I'm having trouble to get the ONLY SALESMAN who have SOLD a product to every CLIENT on the list
Compare the SALESMAN column with the CLIENT_KEY and return the value.
Could be only one salesman or two, I don't know.
This is a long list +8K transactions.
Below is a picture of the database
Thank you
Assuming you only have one table and all clients means all distinct client keys from your table.
SELECT SALESMAN
FROM YourTable
GROUP BY SALESMAN
HAVING count(DISTINCT CLIENT_KEY)=(SELECT COUNT(DISTINCT CLIENT_KEY) FROM YourTable)
The query sums all clients for each salesman, and checks if that number is equal to the sum of all distinct clients in the table.
if client list is a table then :
select salesman
from salesManTable s
group by salesman
having count(distinct clientid) = (select count(*) from clienttable)

MySQL Complex Queries

Hey I've been killing myself trying to figure out how to do these queries. Can someone help me out.
These are the tables I have currently.
BOOKING
HOTEL_NO
GUEST_NO
DATE_FROM
DATE_TO
ROOM_NO
GUEST
GUEST_NO
GUEST_NAME
CITY
ADDRESS
ZIP_CODE
HOTEL
HOTEL_NO
HOTEL_NAME
CITY
ADDRESS
ZIP_CODE
STAR
ROOM
ROOM_NO
HOTEL_NO
ROOM_TYPE
PRICE
And these are the queries I need to do.
-List the guests that have all their bookings (past and present) in the same hotel.
-Create a view VIP-Guest that lists guests who have reservations for only 4 star hotels or
4 star hotels
-Among the VIPs find the guest with the largest total stay (in term of number of days).
Express this as a query with the view and without the view
Can someone help me out?
this should get you started. to post on stackoverflow, you need to come with specific questions or errors or problems. like for the query you posted in the comments up top.... that could be a question in itself: "I have these tables, this one specific goal (question/result set), and I tried this query... it gives me this result or it gives me this error."
BOOKING: HOTEL_NO, GUEST_NO, DATE_FROM, DATE_TO, ROOM_NO
GUEST: GUEST_NO, GUEST_NAME, CITY, ADDRESS, ZIP_CODE
HOTEL: HOTEL_NO, HOTEL_NAME, CITY, ADDRESS, ZIP_CODE, STAR
ROOM: ROOM_NO, HOTEL_NO, ROOM_TYPE, PRICE
all guests and bookings...
-- all guests: select * from guest;
-- all bookings: select * from booking;
select *
from guest
join booking on guest.guest_no = booking.guest_no;
-- which is the same as...
select *
from guest, booking
where guest.guest_no = booking.guest_no;
-- and... your comments query was missing a group by clause
select guest_no, guest_name, count(*) as booking_count
from guest
join booking on guest.guest_no = booking.guest_no
group by guest_no, guest_name;
select guest_no, guest_name, count(distinct hotel_no) as hotel_count
from guest
join booking on guest.guest_no = booking.guest_no
group by guest_no, guest_name
having count(distinct hotel_no) = 1;
and I count(distinct hotel_no) because... they might have 3 bookings at Hotel A and 1 at Hotel B. The basic join would give me 4 rows for that person. I don't care how many bookings. I care how many hotels. So I want to count the distinct occurrences of hotel_no per person (there's that group by) instead of every row.
guests by their stars...
-- so we have to get guest and hotel joined. bc hotel has stars.
-- booking has hotel_no. so... we can use that last query and
-- join in HOTEL to get the star information. in the WHERE you
-- will want to put your filter for the number of stars that you
-- are looking for =4 or >=4 or something like that.
-- you might want to check out DISTINCT to get just a list of names
-- instead of a row for each booking.
number of days they stayed...
-- use the second query.
-- datediff(date_to, date_from) as days_stay gives you the length of stay
-- i don't know what the view is.
-- to get the top length could go two ways... either ORDER BY and LIMIT if there is
-- only one person with the top length (let's say 10 days). if there are many people
-- who have stayed 10 days, you'll need to do a MAX on the days_stay and either join
-- that in or use it in the WHERE as a nested select.
this assumes there is a single highest length of stay. only one person stayed 10 days.
SELECT guest_no, guest_name, datediff(date_to, date_from) days_stayed
FROM vip_guest
join booking on vip_guest.guest_no = booking.guest_no
order by datediff(date_to, date_from) desc
limit 1,1
this should work for many... (i'm not testing these... just kind of looking at it)
SELECT distinct guest_no, guest_name, datediff(date_to, date_from) max_stay
FROM vip_guest
join booking on vip_guest.guest_no = booking.guest_no
where datediff(date_to, date_from) = (
select max(datediff(date_to, date_from)) as days_stayed
from booking )
the nested query gets the maximum stay length of everyone. vip_guest and bookings joined together give us guest and date imfo. we will get all bookings for every vip_guest. so we want to filter it down to where stay lengths == the max stay length. in case a person had multiple 10 day stays (my arbitrary max stay length)... use distinct.
now... thats a good point about the nested query. i don't know what is in your view. it is possible none of the max vip guests had a stay as long as the max stay length. in that case, this query would return nothing.

Sorting deals by their revenue, which is sum of orders table rows in MySQL

Okay I always have questioned this, but never really needed it until today.
Normally, I would grab all deals, go through them each and find the SUM(amount) from all the orders related to this deal. (PHP)
Then I would do a simple uasort() which works fine.
But now I need to do it all with a sql query.
This is what I have tried:
SELECT deals.ID, SUM(orders.amount) AS revenue FROM deals
JOIN orders ON (orders.deal_id = deals.id)
ORDER BY revenue DESC
Now this gives me one row, with a 'random' deal ID, and a bigger number in revenue.
I suspect this number in revenue is the SUM of ALL the amount columns, and not for this particular deal ID.
What I would like is a row for each deal ID, and with the right number in the column revenue, all the rows sorted DESC.
How would this be done? To sort the deals, out from their revenue (custom column) - which is the sum of all the amount columns from the rows for the respective deal id.
you lack GROUP BY clause
SELECT deals.ID, SUM(orders.amount) AS revenue
FROM deals
INNER JOIN orders
ON orders.deal_id = deals.id
GROUP BY deals.ID
ORDER BY revenue DESC
GROUP BY Clause
GROUP BY AGGREGATE FUNCTION

Query to find how many purchases were made this year, For each Artist

I need to list the Artist's Id_no, last name (lname) and how any purchases have been made on their releases this year. The only information about the purchases is different purchase dates.
My code so far :
SELECT id_no, lname, purchasedate AS num_ops
FROM Artist JOIN Sales ON Artist.id_no = Sales.artist
WHERE DATE_SUB(CURDATE(),INTERVAL 1 YEAR) <= purchasedate
However this only returns the Id_no's who have made sales, and I need all even if it is 0. On distrinctl name, and num_ops is a date of their first purchasedate. I need to alter this code to list how many purchases they have been involved in within this year. I've tried using COUNT(purchasedate) however this just returns one row. I wish the returning table to return:
1st Column: The Artist's ID Number
2nd Column: The Aritist's Last Name
3rd Column: The number of people who have purchased their CD's (which is a count of purchasedate)
I am struggling with the 3rd column majorly, and any help would be greatly appriciated.
You need:
a left join
to move the date condition into the ON clause of the JOIN
use the count(*) aggregating function
Like this:
SELECT id_no, lname, count(purchasedate) as num_ops
FROM Artist
LEFT JOIN Sales ON Artist.id_no = Sales.artist
AND DATE_SUB(CURDATE(),INTERVAL 1 YEAR) <= purchasedate
GROUP BY id_no, lname
The LEFT JOIN will ensure a row is returned for every artist even if there are no sales
By moving the date condition into the join that will still return a row for every artist even if there's no sale for the year. If the condition is left in the WHERE clause, that would filter out artists that didn't make sales in the last year.
A key point here is that the join condition may contain conditions unrelated to the keys involved - that's how you get conditional joins, which is what you want here to make the left join still function correctly