Query all users who have watched the same movies in SQL - mysql

I have a simple query that I need to execute which will return all users who have watched the same movies, the date they did so and the number of times each movie was watched. The result should be grouped by movie title.
Say I have movie title: Gladiator and Prometheus. The result should be like:
Gladiator: Watched: 10 times| by: Alan, Bruce, Cecil, Marlon, etc| on: 10/01/2020, 1/11/2019, etc
Prometeus: Watched: 7 times| by: Julia, Baner, Alan, Marlon, etc | on: 10/01/2020, 1/11/2019, etc
I have MOVIE table, USER table, VIEW table
Some infos that might help you understand my problem:
MOVIE has view count row
VIEW has date row
USER has name, etc
My question is a bit similar to this: Query on all the people who has watched the same movies as I did but I wasn't able to adapt the solution to my case.
How can I achieve this?

I am assuming the following table structures:
movie: movie_name, view_count
user: user_name, age, city
view: movie_name, user_name, view_date
The query can be:
select m.movie_name as movie,
m.view_count as watched,
listagg(v.user_name, ',') as by,
listagg(v.view_date) as on
from movie m, view v
where m.movie_name = v.movie_name
group by m.movie_name, m.view_count;
Group by function listagg is available in Oracle's SQL. Microsoft's SQL has similar string_agg, in MySql group_concat etc.

Related

Averaging Movie Ratings in SQL

I have a SQL data base comparing different ratings that critics give to movies. I am trying to find the average of the critics' ratings for each unique movie and then create a new table to display these aggregated ratings.
I tried using the AVG(DISTINCT()) functionality, but it only found the averages by distinct ratings, not by distinct movies. Any help with aggregation would be huge!
ETA:
Sample data example
Movie
Rating
Jaws
1
Jaws
4
Jaws
3
HomeAlone
2
HomeAlone
5
Returned data example
Movie
Rating
Jaws
2.67
HomeAlone
3.5
So average all of the individual ratings by the movie name and then create a new table.
A possible solution is to group by Movie.
SELECT Movie, AVG(Rating) FROM [Table] GROUP BY Movie;
I think this will create the answer you want as the output in a new table
CREATE TABLE new_table AS
SELECT Movie, round(AVG(Rating),2)
FROM table_name
GROUP BY Movie;

How get individual values for recursive query in SQL?

I have a cellphone table where the data as follows,
Desired output:
For example iPhone X is made by two different companies so need to output 2. If the model is manufactured by the same company then the count should 1 not 2. If I run the query recursively I get a total count rather than individual count. I'm new to SQL. Could you please correct my query,
select count(distinct company) from cellphones where model in (select model from cellphones)
Thanks in advance.
This looks like a simple aggregation:
select model, count(*) as num_rows,
count(distinct country) as num_countries
from cellphones
group by model

How to show a data using JOIN in SQL (PHP)

Select maintable.name FROM maintable
JOIN genres genre1 USING (tmdb_id)
JOIN genres genre2 USING (tmdb_id)
WHERE genre1.genres_name = 'Action'
AND genre2.genres_name = 'Drama'
group by maintable.name
Here genres is table name. genres_name is column name. genres1 and genres2 are just nor a table name, nor a column name, they are just random name in the code.
This is my code, now How do i display all genres_name?
The genres is like:
tmdb_id genres_name
1 Action
1 Crime
1 Drama
2 Horror
2 Comedy
2 Drama
The main table isl ike
tmdb_id movie_title
1 The Dark Knight
2 Logan
3 Wonder Woman
Let me know, if you need more information. (Please do not ask to show, what i tried. Trust me, it will make the question more confusing)
I want to echo the genres like:
The Dark Knight - Drama, Action, Crime
Of course, you need to use group_concat:
Select maintable.movie_title, group_concat(genres.genres_name) AS genres_name
FROM maintable
JOIN genres USING (tmdb_id)
GROUP BY maintable.tmdb_id
HAVING find_in_set('Action', genres_name) AND find_in_set('Drama', genres_name)
See demo here.
Note: How does find_in_set works, please see official doc.
I would try something like this. But this is the best I can do guessing at it in my head... ( sorry for any mistakes )
$Sql = "SELECT
m.name,
GROUP_CONCAT( g.genres_name ) as genres_list
FROM
maintable AS m
JOIN
genres AS g USING (tmdb_id)
WHERE
g.genres_name IN('Drama', 'Action')
GROUP BY m.tmdb_id";
MySQL GROUP_CONCAT() function returns a string with concatenated non-NULL value from a group.
http://www.w3resource.com/mysql/aggregate-functions-and-grouping/aggregate-functions-and-grouping-group_concat.php
Also note GROUP_CONCAT has a setting for the length, I don't recall what that is or how to change it, but it bit me in the butt one time. Basically it will truncate the list after a certain size, so be cautious of that.
See here: MySQL and GROUP_CONCAT() maximum length
AS I said I haven't tested this, but it seems you have a many to one relationship. Records in the maintable can have many related records in the genres table. Therefor, you should be able to group them on that relationship. Normally this would return 1 record for each pair ( same record in main table different in genre ) Without the group. The Group Concat allows you to compress that into a comma separated list.

PostgreSQL how to add aggregated column to existing query

I have project to analyze premier league stats 2011/12 and I wanna operate on one column (scored goals) when playerd from start(1) or substituted(0) and I wanna show name,surname and sum of scored goal when player started game and substituted. which looks like this only for score at home:
and I wanna make this looks like this
I know that this subquery is wrong because it summing all goals not by the player.
How to make it looks separate like for van persie 28 scored from start(1) and from bench (0) supposed to be 2?
erd diagram if you wanna see:
https://postimg.org/image/u46b6lve3/
Your sub query counts all the goals because you are not passing player id to it, try changing the query to the following:
select player_id as pid, //other columns
select sum(goals) from projekt.statictics where starts = 0 and player_id = pid) as wyjazd
from //conditions
You may need to change the column names based on what they are named in the tables.

Confused on SQL assignment

We are doing a database query in class. And it's using relational keys. I don't know how to get the query to run. Here is what is says.
For each movie, list its number and title, along with the number and name of the actors who appeared in it.
This is what I have, but it doesn't work
SELECT `Movie`,`Movie_ID`,`ActorNum` FROM `Movies`
Union
Select Actor.Fname, Actor.Lname FROM Actor
;
Im not sure what all the column names are but if ActroName would be the actors name would this be what you are looking for?
SELECT Movies.Movie,
Movies.Movie_ID,
Movies.ActroNum,
Actor.ActroName
FROM Movies
JOIN Actor ON
Actor.ActroNum = Movies.ActroNum