Most common value for multiple fields - mysql

I have a table which contains ratings. Included with the rating is data the user provided such as the language that the linked series belongs to.
id series_id language quality type rating
1 18 1 2 1 20
2 18 2 3 2 13
3 18 1 1 2 25
4 18 1 3 5 8
5 18 3 1 1 17
6 18 3 3 2 9
What would be the best way to find the most common value for language, quality and type, for a certain series_id?
In other words, if I was looking at series 18, it would return the following as those numbers occur the most.
language: 1
quality: 3
type: 2

It's probably easiest to do it as three separate SELECT statements:
SELECT language
FROM MyTable
WHERE series_id = 18
GROUP BY language
ORDER BY COUNT(*) DESC LIMIT 1
SELECT quality
FROM MyTable
WHERE series_id = 18
GROUP BY quality
ORDER BY COUNT(*) DESC LIMIT 1
SELECT type
FROM MyTable
WHERE series_id = 18
GROUP BY type
ORDER BY COUNT(*) DESC LIMIT 1

Related

Count(*) MySQL - Need to display how many entities have a count of 4 or higher

I have a linking table in MySQL which has the id's of people in one column and the id's of media that they have appeared in.
I have run a query:
SELECT person_id,COUNT(*)
FROM person_media
GROUP BY person_id;
This has returned:
person_id COUNT(*)
1 7
2 4
3 9
4 5
5 9
6 12
7 12
8 3
9 1
10 8
11 8
12 9
13 3
14 1
15 4
16 3
17 3
18 1
19 8
20 1
21 4
What I am looking to do is to count how many people have a COUNT(*) of 4 or more. I can't seem to find a way to do it, but I know it has to be possible. I'm relatively new to MySQL, so be gentle :)
Just use HAVING
SELECT person_id,COUNT(*)
FROM person_media
GROUP BY person_id
HAVING COUNT(*)>=4
The inner query gets all those with a count of 4 and more.
The outer query counts how many that are:
select count(*)
from
(
SELECT 1
FROM person_media
GROUP BY person_id
HAVING count(*) >= 4
) tmp

can rank count duplicate value as a specific value in dax?

my table in tabular model in like below:
Id Name Score
1 mark 20
2 john 10
3 jack 20
4 Jess 20
5 Brad 9
If I use rankx it rank my table as:
Id Name Score Rank
1 mark 20 1
2 john 10 4
3 jack 20 1
4 Jess 20 1
5 Brad 9 5
is there anyway can I have the column rank as below:
Id Name Score Rank
1 mark 20 1
2 john 10 4
3 jack 20 2
4 Jess 20 3
5 Brad 9 5
Here are three options.
Rank1 = RankX ( 'Table', 'Table'[Score] + ( 0.1/'Table'[ID] ),, DESC )
Rank2 = RankX ( 'Table', 'Table'[Score] ,, DESC, Dense)
Rank3 = RankX ( 'Table', 'Table'[Score] ,, DESC, Skip)
In [Rank1] the [ID] value is used to break the ties.
In [Rank2] and [Rank3] the ties are not broken. The Skip or Dense parameters determine if rankvalues are skipped after a tie or not.

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

How to select more rows with MySQL after sorting?

I have a table which have 10 results. Let's say the following:
id user number
-- ---- ------
1 user1 10
2 user2 5
3 user3 30
4 user4 45
5 user5 5
6 user6 22
7 user7 10
8 user8 40
9 user9 90
10 user10 65
I basically want to sort them, by the 'number' value.
So it should be something like this:
SORT id user number
---- -- ---- ------
1 2 user2 5
2 5 user5 5
3 1 user1 10
4 7 user7 10
5 6 user6 22
6 3 user3 30
7 8 user8 40
8 4 user4 45
9 10 user10 65
10 9 user9 90
After it's been sorted, I want to select * from (for example) the one with id = 6 (which's number is 22) and 2 other results which is above it (in this case: id = 7 and 1) and 2 other results under it (in this case: id = 3 and 8).
So the return result should be something like this, when I'm searching for id = 6:
SORT id user number
---- -- ---- ------
3 1 user1 10
4 7 user7 10
5 6 user6 22
6 3 user3 30
7 8 user8 40
I could esaily do this on server side, if I select everything, however there will be a huge data amount in here, so I'd rather just select those, which are appropriate to my search.
Is there any way to do this with MySQL?
Here is a typical way to get what you want:
select t.*
from ((select t.*
from table t
where number <= (select number where id = 6 limit 1)
order by number desc
limit 3
) union all
(select t.*
from table t
where id > (select number where id = 6 limit 1)
order by number asc
limit 2
)
) t
order by number;
This assumes that when duplicates appear, you still want 5 rows output. It also assumes that less than five rows is ok for the first two or last two rows. An index on id would help performance of this query.
Get UNION of 2 queries:
Get all records <= the desired number, in your case 22. Sort them in descending order and get top 3 results.
Get all records > the desired number in ascending order and get top 2 results
Get the UNION of these 2 results and order them in ascending order
HTH
EDIT: My idea to post this answer was to give an algorithm on how to approach such queries. Instead of selecting on number, you can get all records based on id and then UNION them.

complex sql query in sql server 2008

I have 4 major tables in my database.
Season --> seasonID
Trials --> trialID
Competition --> CID,name
Camps --> campID,DivisionID(FK)
Divisions ---> DivisionID
Contestants --->ContestantID
Now a contestant belongs to / are members of a divisions.
Then a division belongs to a camp.
All this leads to my Performance table.
PERFORMANCE TABLE
SeasonID|TrialID|CampID|DivID|CompetionID|CtestantID|Score1 |Score2 |Total
1 1 1 1 1 1 20 20 40
1 1 1 1 2 1 20 15 30
1 2 1 1 1 2 10 5 15
1 2 1 1 2 2 5 5 10
1 2 1 1 1 1 10 30 40
1 2 1 1 2 1 20 10 30
How can I query this performance table to give me the competition name, total score and rank (ranking over total score) of each contestant in each competition by trials and by seasons?
Example:
In season 1 and trial 2 I want to have:
SeasonID| TrialID | ContestantID| Competition | TotalScore | Rank
1 2 1 1 40 1
1 2 2 1 15 2
1 2 1 2 30 1
1 2 2 2 10 2
How do I go about this? I have tried table variables, pivot and joins but I can only rank by competitions, but I don't how to aggregate the results to get the result above!
I'm not exactly sure how you calculated your desired results. I think this is what you are after but, if so, the TotalScore in the desired results of your question should be 10 for the last record, not 20.
SELECT SeasonID, TrialID, ContestantID, CompetitionID, Total,
DENSE_RANK() OVER(PARTITION BY CompetitionId ORDER BY Total DESC) AS [Rank]
FROM PerformanceTable