list the top 5 users by time spent watching videos - mysql

Table1:
***Publisher*** with following columns:
o **Publisher_id** ; unique ID for each publisher
o **Video_id** ; unique ID for each video
o **Video_duration**; in minutes for each video
Table2:
***Consumer*** with the following columns:
o **Video_id** ; unique ID for each video
o **User_id** ; unique ID for each customer
o **Dateid** ; the date on which the consumer watched a video
Example:
Publisher_id Video_id Video_duration
1 1 2
1 2 3
1 3 2
1 4 5
2 6 2
2 7 2
2 8 20
3 10 2
3 11 3
3 12 3
1 23 100
5 9 8
Video_id User_id Date_id
1 44 20210615
2 44 20210615
11 33 20210614
6 44 20210612
I think I mostly need to figure out how to sum up the time spent watching videos per user_id and then to corroborate that using a limit 5 select, but I can't figure how to combine these 2 selects

I'd try like this:
SELECT SUM(Video_duration)
FROM Consumer
INNER JOIN Publisher USING (Video_id)
GROUP BY User_id
ORDER BY SUM(Video_duration) DESC
LIMIT 5

Related

MySQL: return first row from join

I have 2 tables "quizzes" & "users", I need to return list of first user took each quiz, with quiz_id:
tables structure
"quizzes" structure:
id name
1 England
2 france
3 Japan
4 USA
5 UAE
6 Sweden
7 Italy
8 Brazil
9 South Korea
10 India
"users" structure:
id user_id quiz_id
1 1 1
2 1 2
3 2 1
4 3 4
5 1 4
6 5 9
7 2 9
8 3 8
9 3 9
10 3 7
I need to run query to return first "user_id" took each "quiz", (order by users.id ASC)
expected results:
quiz_id user_id
1 1
2 1
4 3
7 3
8 3
9 5
thanks,
You first group by quiz and pick minimal id and then select based on those ids:
select quiz_id, user_id
from users
where id in(select min(id) from users group by quiz_id)
Select quiz_id, user_id
from users
Where (quiz_id, id) in
(
Select quiz_id, min(id) id
From users
Group by quiz_id
)

HIVE how to limit number of entries in group

I'm learning HIVE these days and meet some problems...
I have a table called SAMPLE:
USER_ID PRODUCT_ID NUMBER
1 3 20
1 4 30
1 2 25
1 6 50
1 5 40
2 1 10
2 3 15
2 2 40
2 5 30
2 3 35
How can I use HIVE to group table by user_id and in each group order the records by DESC order of NUMBER and in each group I want to keep up to 3 records.
The result I want to have is like:
USER_ID PRODUCT_ID NUMBER(optional column)
1 6 50
1 5 40
1 4 30
2 2 40
2 3 35
2 5 30
or
USER_ID PRODUCT_IDs
1 [6,5,4]
2 [2,3,5]
Could someone help me ?..
Thanks very much!!!!!!!!!!!!!!!!
try this,
select user_id,product_id,number
from(
select user_id,product_id,number, ROW_NUMBER() over (Partition BY user_id) as RNUM
from (
select user_id, number,product_id
from SAMPLE
order by number desc
) t) t2
where RNUM <=3
output
1 6 50
1 5 40
1 4 30
2 2 40
2 3 35
2 5 30
hive version should be 0.11 or greater, may I know if your version is lower

SQL query syntax to retrieve data from 2 tables

I have 2 tables in Mysql: authors and articles.
I have authors with several articles for each of them.
I need sql query with the result of this query:
retrieve 3 authors ordered by age with all articles which belong each of them.
In my example it will be
ID FIRSTNAME LASTNAME AGE AUTHORID TITLE PRICE
6 Salido Gomes 90 6 All 3 1
6 Salido Gomes 90 6 All 3 33
6 Salido Gomes 90 6 All 3 3
5 Vitora Mantora 45 5 Total 3 99
5 Vitora Mantora 45 5 Total 3 33
5 Vitora Mantora 45 5 Total 3 12
3 Joe Smith 43 3 Python 5
3 Joe Smith 43 3 Python 2 22
3 Joe Smith 43 3 Python 3 44
3 Joe Smith 43 3 Python 4 12
3 Joe Smith 43 3 Python 5 67
http://sqlfiddle.com/#!2/718c4/1
I use
select * from authors join articles on authors.id = articles.authorId
join (select authors.id from authors order by age DESC limit 3) as t
on t.id = authors.id
with wrong result
ID FIRSTNAME LASTNAME AGE AUTHORID TITLE PRICE
6 Salido Gomes 90 6 All 3 1
6 Salido Gomes 90 6 All 3 33
6 Salido Gomes 90 6 All 3 3
If you only want to include authors who have written any articles in the top 3 oldest authors try doing a join between authors and articles in the sub query to get the 3 oldest authors, and DISTINCT to eliminate duplicates:-
SELECT *
FROM
(
SELECT DISTINCT authors.id, authors.firstname, authors.lastname, authors.age
FROM authors
JOIN articles
ON authors.id = articles.authorId
ORDER BY authors.age
DESC
LIMIT 3
) author_sub
JOIN articles
ON author_sub.id = articles.authorId

Most recent distinct record from a joined MySQL table

I have two tables, one of a list of competition results, and one with ELO ratings (based on previous competitions).
Fetching the list of competitors for an arbitrary competition is trivial enough, but I also need to get the most recent rating value for them.
score:
id | eventid | competitorid | position
1 1 1 1
2 1 2 2
3 1 3 3
4 2 2 1
5 2 3 2
6 3 1 1
7 3 3 2
8 3 2 3
rating:
id | competitorid | rating
1 1 1600
2 2 1500
3 3 1500
4 2 1600
5 3 1590
Expected output for a query against score.eventid = 3 would be
id | competitorid | position | rating
6 1 1 1600
7 3 2 1590
8 2 3 1600
At the moment my code looks like:
SELECT score.scoreID, score.competitorID, score.position,
rating.id, rating.rating
FROM score, rating
WHERE score.competitorid = rating.competitorid
AND score.eventid = 3
ORDER BY score.position
which gives an output of
id | competitorid | position | rating.id | rating
6 1 1 1 1600
7 3 2 2 1500
7 3 2 4 1590
8 2 3 3 1500
8 2 3 5 1600
basically it's showing the data from the score table for that correct event, but giving me a row for every rating available against that competitorID unfortunately I have no idea where to build in the DISTINCT statement or how to limit it to the most recent result.
MySQL noob, and managed DISTINCT statements, but not with joins. Unfortunately most previous questions seemed to deal with getting distinct results from a single table, which is not quite what I'm after. Thanks!
One way to get the rating is with a correlated subquery:
SELECT s.scoreID, s.eventID, s.competitorID, s.position,
(select r.rating
from rating r
where s.competitorID = r.competitorID
order by r.id desc
limit 1
) as rating
FROM score s
WHERE s.eventID = 3
ORDER BY s.position;
I'm not sure what ratingprid is, so this only includes the rating.

Select min/max from multiple items

I'll try to explain it as simple as possible:
First some database structure with dummy data.
Structure
tb_spec_fk
feature value
-----------------
1 1
1 2
1 3
1 4
1 5
2 2
2 3
3 1
3 4
4 2
4 3
4 4
5 1
5 3
5 5
6 3
6 5
tb_spec_feature
feature_id filter
------------------
1 2
2 2
3 2
4 2
5 1
6 0
tb_spec_value
value_id name
----------------
1 10
2 20
3 30
4 40
5 50
Now, what I want is the follow result
Result
feature_id min_value max_value
---------------------------------
1 10 50
2 20 30
3 10 40
4 20 40
But how?
Logic
Get from the tb_spec_feature where "filter" equals 2 the highest and lowest values which are present in the tb_spec_value table and connected together trough the tb_spec_fk table.
My attemps
A lot! But I'll spare you :)
SELECT
f.feature_id AS feature_id,
MAX(value.name) AS max_value,
MIN(value.name) AS min_value
FROM tb_spec_feature AS f
JOIN tb_spec_fk AS fk ON f.feature_id=fk.feature
JOIN tb_spec_value AS value ON fk.value=value.id
WHERE f.filter=2
GROUP BY f.feature_id
The two JOIN statements "link" the a feature to a value. GROUP BY groups all rows with the same feature id, and then you can take the min or max or any other aggregate function on those columns.
Demo
Here is how you can do it
select
tsf.feature_id,
tsvl.name as Min_Value,
tsvr.name as Max_Value
from tb_spec_feature as tsf
inner join (select feature , MIN(value) MinV,MAX(value)MaxV from tb_spec_fk group by feature order by feature)as tsfkl on tsfkl.feature = tsf.feature_id
left join tb_spec_value as tsvl on tsvl.value_id = tsfkl.MinV
left join tb_spec_value as tsvr on tsvr.value_id = tsfkl.MaxV
where tsf.filter = 2
group by tsf.feature_id
Output
feature_id | Min_Value | Max_Value
---------------------------------
1 | 10 | 50
2 | 20 | 30
3 | 10 | 40
4 | 20 | 40
Fiddle Demo