GROUP BY one variable but display multiple SQL - mysql

I'm trying to GROUP BY one categorical variable but also show another corresponding variable in my output in SQL. Here is what the table looks like:Original Data Table
There are three main variables I'm working with here: Game, Platform and Week. Week variable states the position of the game under the global charts, so 1 would mean its the number 1 game. I want to find the game with the most weeks inside the top 10 BY platform, so I'm trying to get my table to look like this:
Platform | Game | Most_weeks_top10
Right now, I tried the following steps:
SELECT platform, game, COUNT(*) AS total
FROM global_weekly_charts_2013_2014
WHERE week <= 10
GROUP BY game, platform;
Which returns this:
Table Grouped By Platform AND Game
However, I only want the game, platform and total weeks of the game with max number of weeks in top 10. I tried
SELECT game, platform, Max(total) OVER (PARTITION BY platform)
from the derived table but did not get the desired output. I feel like the solution is right there and not that difficult but I can't seem to get the answer.

please add “ORDER BY total DESC LIMIT 10”.try again

Related

MySQL query for an MLB database

There are 5 tables: mlb_batting, mlb_manager, mlb_master, mlb_pitching, mlb_team.
Find the top 10 (highest) “strike outs per walk” statistic for all pitches with at least 1 walk that played in at least 25 games. You should display their first name, last name, and K/BB statistic. K/BB is computed by dividing the number of strike outs by the number of walks (“base on balls”). You will need to use “limit” in MySQL (not talked about in class or notes – you will have to search how to do it). I would like this query done 2 different ways. One that only looks at the 25 games and 1 walk on a per stint basis. That is, if they played for two different teams (two different stints) then you would count those separate. And the other query should combine all the stints they had. That is, if they played for two different teams you would add up their games and walks.
My solution is:
SELECT NAME_FIRST, NAME_LAST, SUM(strikeouts) / SUM(walks) AS KS_PER_BB
FROM mlb_master
JOIN mlb_pitching
ON mlb_master.player_id = mlb_pitching.player_id
WHERE walks >= 1 AND games >= 25
GROUP BY name_first, name_last, mlb_pitching.stint
ORDER BY KS_PER_BB DESC
LIMIT 10;
I am wondering if this solution is better for the first way my professor wants it done or the second way, if any.
This solution is appropriate for the first query because by having GROUP BY stint, each stint is considered different for each player.
For the second way, could I remove the stint column from the GROUP BY clause so that it groups the records for a particular player together, regardless of the different stints they played for?
Would this result in the sum of all their walks and strikeouts from all their stints being used to calculate the KS_PER_BB statistic, giving you the combined total for each player?

time difference grouped by 2 different parameters

I am wondering if anyone could help me with a current problem i am having.
I have a database table which has the following columns
enter image description here
I have various different Areas which can have the same product and what i am looking to do is to sum the difference for all the different products in their respective areas. for example in the table above i would get the below results ( product 1 in area 1 total is 3 minutes etc)
Area Product Time
Retail Product 1 00:03:00'
Packing Product 2 00:02:00'
The start and stop columns have the DATETIME datatype
I have tried sum(timediff(stop,start)) and group it by product but this does not appear to work.
Consequently I also want to sum the difference based on the area bit no matter what I put in i always get 181
I would appreciate any help on this
All you need to do is group by your area and product, and sum the times for each. The answer here should help. For your query, you'll just need something like this:
select
area
,product
,SEC_TO_TIME(SUM(TIME_TO_SEC(`stop`) - TIME_TO_SEC(`start`))) as timediff
from products_in_area
group by area, product
If you're not tied to showing the end result as a TIME value (e.g. you can do the formatting in your calling application), your query would look a bit cleaner:
select
area
,product
,SUM(TIMESTAMPDIFF(MINUTE, `start`, `stop`)) Minutes
from products_in_area
group by area, product

how to random one result from mysql results by using one sql

I use mysql select some data for player. and the result is a list while I just want a random one.
the following sql is syntax error after limit 1
select * from tb_rank where score<=150 and score>= 50 and power>=80 and power<=120
limit 1,(select round(rand()*(select count(*) as num from tb_rank where score<=150 and score>= 50 and power>=80 and power<=120)))
50.000 people are a lot if you have them in front of you. But you are talking about crunching numbers on a computer. Here 50.000 is nothing.
Sorting would just take additional time and it is not necessary as you want a random player that has your score ±50 and your power ±20%. A random player of a sorted list is still a random player. It wouldn't make any difference.
Iterate over your playerlist, build a new list of players that have a valid score and power. Then pick a random element of that new list.
On my average laptop this takes less than 5 microsconds.

How to find the smallest difference between two columns in Mysql?

I have a table in my database which contains data for recording games between people. I wanted to have a query that would return the game which was closest between two players, ie, the smallest difference between the two scores recorded, regardless of who won. I have started with something like this as a query, but I can't quite get what I want.
SELECT recorder_score, opponent_score
from games
where recorder_id = $recorder_id
order by (recorder_score - opponent_score)
limit 1
The above obviously would only return the closest game that the person submitting the game has won, but as I mentioned, I want the closest game regardless of who wins. What will be the best way of doing this?
Use ABS in Order by to get the closest game between two players
SELECT recorder_score, opponent_score
from games
where recorder_id = $recorder_id
order by ABS(recorder_score - opponent_score)
limit 1

Grabbing the top (or lowest) result from a row sorted by groups of ids?

So the title might be a little confusing because I'm a little confused myself. What I am trying to do is grab the lowest time of a completed map from a mysql database to display in a table on a website.
The maps are sorted by an id (MapID) with a time on the same row.
Example of a couple of rows with same MapID (the time is in seconds):
MapID: Time:
1 28.234
1 24.874
1 18.244
2 213.521
2 164.6
So I'd like to grab the lowest time from each MapID and be able to display that on a table in my site.
How would I query something like this?
Try something like:
SELECT MapId, Min(Time) AS Time
FROM mytable
GROUP BY MapId
Output:
MapId: Time
1 18.244
2 164.6