Calculate avg for field using data from another table MS Access - sql

I have 3 tables :
Film.rating is the average of ratings for that film from the Rate table.
In SQL, to calculate film rating I would do:
SELECT AVG(Rate.rating)
FROM Rate, Film
WHERE Rate.uidFilm=Film.uidFilm;
I do not know how to write this in MS Access and where I should put this (or maybe another?) formula?
How do I 'connect' Film.rating with the calculation above?
In constructor for the Film table?

You need a GROUP BY in your query. Try this
SELECT Film.Title, AVG(Rate.rating)
FROM Rate
INNER JOIN Film ON Rate.uidFilm = Film.uidFilm
GROUP BY Film.Title;
This should give you the average ratings of all movies with their title.

Related

how to make a query in SQL from the result of another avg(query)

I wonder if someone could help with this please, I'm new at SQL and I am trying to do this.
I have the following 3 tables/columns
Bookings(roomID,bookingID,guestID)
Room(rooCost,roomID)
Guests(name,lname ext)
I guess we can leave Guest table alone for this task.
So in essence I would need to extract the avg(roomID) and how much this particular room costs from a different table/column combination.
Please forgive my pseudo code bellow :)
Now, I'd like to know the
avg room(Booking.roomID) rented by all guests and its related cost(Room.roomCost)
So I guess something like:
query the avg(booking.roomID) (btw this would give me an int 10)
then
query the cost of room(10) from Room.roomCost
I have try so many combinations!, it's unbelievable!! Once I was nearly there I think, but I was getting like +400 lines response from a 20 line table.??
Appreciated any help.
Many thanks
If you want to get room wise average cost you can have with below query:
select roomid, avg(roocost) from room
group by roomid
If you want to have the average value of rooms rented by any guest then use below query:
select roomid, avg(roocost) from room
where roomid in (select roomid from bookings)
group by roomid
To calculate guest wise room wise average cost:
select guestid,roomid, avg(roocost) from room r inner join booking b
on r.roomid=b.roomid
group by guestid,roomid
To calculate guest wise room wise average cost (withe guest name):
select name guestname,guestid,roomid, avg(roocost) from room r inner join booking b
on r.roomid=b.roomid
inner join guests g on g.guestid=b.guestid
group by name,guestid,roomid

How can I use COUNT in the same query with SUM()?

Ok, I have three tables: theater, spectacles and tickets. A theater has one or more spectacles and for a spectacle were sold one or more tickets* So, id_theater is foreign key in TABLE SPECTACLES and id_spectacle is foreign key in TABLE TICKETS. In table TICKETS, I have ticket_price and id_ticket and with count(id_ticket) I calculate the number of tickets sold for spectacles. And each theater has one or more spectacles. The value for spectacles is calculated as: count(tickets.id_ticket) * tickets.ticket_price. After I calculate the value for spectacles, I need to gather the value of spectacles to find the each value of theater and for this I use SUM().
I need to write a query where I need to calculate the total value obtained by each theater and to be sort by value. For this I need to use the SUM() function. I tried so many different options, but I have different errors. When I tried to run this, this error appeared :
SQL Error: ORA-00937: not a single-group group function
SELECT
theater.id_theater,
SUM(count(tickets.id_ticket) * tickets.id_ticket) total_theater
FROM spectacles, theater
GROUP BY theater.id_theater
ORDER BY total_theater DESC;
For this problem I must use SUM, but I really don't know what do to at this point and would appreciate any help.
You can use normal INNER JOIN and sum as follows:
SELECT theater.id_theater,
SUM(tkt.ticket_price) total_theater_tkt_price
FROM spectacles s
join theater t on t.id_theatre = s.id_theatre
JOIN tickets tkt on tkt.id_spectacle = s.id_spectacle
GROUP BY t.id_theater
ORDER BY total_theater_tkt_price DESC;
Note: Always use the standard ANSI-JOINS.

Issue creating calculated field column in SQL

This is the SQL I have so far:
SELECT RESERVATION.RESERVATION_ID, RESERVATION.TRIP_ID, CUSTOMER.CUSTOMER_NUM,
RESERVATION.NUM_PERSONS,RESERVATION.TRIP_PRICE
FROM RESERVATION, CUSTOMER
WHERE NUM_PERSONS > '4';
I need to create a column named TOTAL_PRICE that calculates the price for all the reservations with more than 4 people. I've tried multiple different ways and I'm still really confused.
The code I have works and it shows all reservations that have more than 4 people attending. But I need the price for each reservation to be calculated in a separate column.
I think you just want an aggregation query:
SELECT SUM(r.TRIP_PRICE)
FROM RESERVATION r
WHERE NUM_PERSONS > 4;

SQL Join query brings multiple results

I have 2 tables. One lists all the goals scored in the English Premier League and who scored it and the other, the squad numbers of each player in the league.
I want to do a join so that the table sums the total number of goals by player name, and then looks up the squad number of that player.
Table A [goal_scorer]
[]1
Table B [squads]
[]2
I have the SQL query below:
SELECT goal_scorer.*,sum(goal_scorer.number),squads.squad_number
FROM goal_scorer
Inner join squads on goal_scorer.name=squads.player
group by goal_scorer.name
The issue I have is that in the result, the sum of 'number' is too high and seems to include duplicate rows. For example, Aaron Lennon has scored 33 times, not 264 as shown below.
Maybe you want something like this?
SELECT goal_scorer.*, s.total, squads.squad_number
FROM goal_scorer
LEFT JOIN (
SELECT name, sum(number) as total
FROM goal_scorer
GROUP BY name
) s on s.name = goal_scorer.name
JOIN squads on goal_scorer.name=squads.player
There are other ways to do it, but here I'm using a sub-query to get the total by player. NB: Most modern SQL platforms support windowing functions to do this too.
Also, probably don't need the left on the sub-query (since we know there will always be at least one name), but I put it in case your actual use case is more complicated.
Can you try this if you are using sql-server?
select *
from squads
outer apply(
selecr sum(goal_scorer.number) as score
from goal_scorer where goal_scorer.name=squads.player
)x

Write a single SQL Query to show list of movies that have been rented and at the same time not rented by customers in MS Access

Movie Table has the following columns:
Movie (Movie_ID, Movie_Title)
Customer Table has the following columns:
Customer (Cus_No, Cus_FName, Cus_LName)
Rental Table has the following columns:
Rental (Rental_ID, Cus_No, Movie_ID)
Now this is the question:
By using a single SQL Query (in MS Access), List the names of all movies and for all movies list the names of all customers who have hired it. If a movie has never been hired then list it anyway. Sort the final result by movie name and customer last name.
This is how I approached
SELECT Movie_Title, Cus_LName, Cus_FName
FROM Movie, Customer, Rental
WHERE Movie.Movie_ID = Rental.Movie_ID AND NOT (Movie.Movie_ID = Rental.Movie_ID)
AND Customer.Cus_No = Rental.Cus_No AND NOT (Customer.Cus_No = Rental.Cus_No)
ORDER BY Movie_Title, Cus_LName;
But, I am getting blank result.
How would the SQL Query look like in MS Access for this question?
The reason you get no results is that your conditions are contradictory:
You have:
Movie.Movie_ID = Rental.Movie_ID AND NOT (Movie.Movie_ID = Rental.Movie_ID)
... but these two conditions can never be true at the same time, yet you require them to be with AND.
What you are really looking for is outer joins.
SELECT Movie_Title, Cus_LName, Cus_FName
FROM (Movie
LEFT JOIN Rental ON Movie.Movie_ID = Rental.Movie_ID)
LEFT JOIN Customer ON Customer.Cus_No = Rental.Cus_No
ORDER BY Movie_Title, Cus_LName
This way a Movie record will still be listed even if the condition in the first ON clause has no match with a Rental record. In that case the SQL engine will fill up the missing values for the customer name with null. That is exactly what the LEFT JOIN is for. With WHERE you would lose out on such Movie records.