MS ACCESS calculate percentage - ms-access

AS a newcomer I am trying to get my head around MS Access to query
The database is a racing one and in the results I want to find the total times a trainer has a favourite that wins as a percentage.
The trainer has trained 22 favourites and 9 have won.
I have written the SQL to get the 9 won figure but struggling to calculate the percentage of 9/22. What is the calculation to add to the query
[2020].FPos = finishing position & [2020].Favs = Favourite type
Total PerCent ???
===========================
SELECT [2020].FPos, [2020].Trainer, [2020].Favs, [2020].Favs
FROM 2020
WHERE ((([2020].FPos)="1") AND (([2020].Trainer) Like "*Eve *")
AND (([2020].Favs)="Fav"));

Answering my own question as it might be useful for others
SELECT 2020.Trainer, Count(2020.Favs) AS [Count of Fav with Win]
FROM 2020
GROUP BY 2020.Trainer, 2020.Favs, 2020.FPos
HAVING (((2020.Favs)="Fav") AND ((2020.FPos)="1"));

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?

SQL Update Query - Get average of multiple ratings and then transfer that number onto a different table

in this problem I have 2 tables, one that has a list of game reviews with a rating 1-5, and another is a list of video games. I want to find the average rating of each game and then insert those numbers into their respective places on another table. I'm not sure where to approach this whether using JOIN would be more appropriate or any other technique.
I understand that in the code below I'm providing the general average as oppose to the specific game rating.
UPDATE GamesList
SET GamesList.AvgRating = AVG(GameReviews.Rating)
FROM GameReviews, GamesList
WHERE GamesList.GameTitle = GameReviews.GameTitle
What I'm trying to achieve here would look like this
GameTitle
Average Rating
GTA
4.3
Witcher
4.6
From a table like this
Game Title
Rating
GTA
4
GTA
4
GTA
5
Witcher
4
Witcher
5
Witcher
5
Any advice would be much appreciated.
I suggest not persisting your average data in a formal table. Instead, just use a query/view to generate the averages:
SELECT gl.GameTitle, COALESCE(AVG(gr.Rating), 0) AS AvgRating
FROM GamesList gl
LEFT JOIN GameReviews gr
ON gr.GameTitle = gl.GameTitle
GROUP BY gl.GameTitle;

how to compute percentage in mysql/sql when 2 group by conditions are present

id title count organizer
1 music 4 2
2 sports 6 2
3 music 2 3
I have a derived table with the above structure. I need to compute the percentage of the number of events of each organizer for each title. i.e. I'm expecting a result like:
organizer title percentage
2 music 40%
2 sports 60%
3 music 100%
The way I'm doing it without organizer in consideration produces the percentage aggregating all the values easily but introducing organizer messes it all up. Can anyone help me here??
Taking your derived table as the actual data (may not be optimal) you could:
select to.organizer, to.title, 100.0*to.count/o.count as percentage
from
(select organizer, sum(count) as count from derivedtable group by organizer) o
inner join
derivedtable to
on
to.organizer = o.organizer
It works by summing the data per organizer to get a total, and joining this back to the data you have so you can do the particular event-organizer count divided by the total count for that organiser
There might have been a simpler way to do it with your source data, as is you'll have to plug your query in that creates your derived table, possibly making it messy. Probably simplest to do as a CTE, but do try to include original source data and "query so far" next time you ask a question, otherwise we have to build a solution on top of some solution we know nothing about and the result might not be optimal
You can try below - using scalar subquery
DEMO
select organizer,title,count*100/(select sum(count) from tablename b where a.organizer=b.organizer)
from tablename a
OUTPUT:
organizer title percentage
2 music 40.0000
2 sports 60.0000
3 music 100.0000

How to perform this MySQL query given this relation diagram

I have the following relation diagram where arrows represent Foreign Keys. The word in the blue is the table name and the words below are column names.
My question is how I could extract the following data from this table:
-what is the GPA of the student with ID=1?
-what are the average GPAs for students by department?
Given that: there are only
five letter grades with values A=4, B=3, C=2, D=1, and F=0, and GPA is the sum of
course credits x course grade value divided by total credits x 4. (so takes.grade is an int from 0-4 inclusive).
I have been trying to figure this out for hours with no avail. Could anyone steer me in the right direction?
Thanks for any help.
Ok, I've actually had to do this for a client over 15 yrs ago, and did for the entire database of all students, not as difficult once you have the pieces.
Without your exact queries as you want guidance.
Start with a single query that pulls data into a TEMPORARY table
CREATE TEMPORARY TABLE AllStudentsCourses
SELECT blah... from blah... join blah.. where... order by ...
A list of all classes a person has signed up for, and while you are at it, have columns computed at the PER-CLASS BASIS the grade earned A-F. You'll also need the credit hours of the class too as that is basis of computing a GPA.
a 3 hour course with an A gets 3 cr hrs towards GPA.
a 6 hour course with an A gets 6 cr hrs towards GPA.
a 6 hour course with a B gets LESS weighted value towards GPA
and you'll need to roll aggregates up.
Now, once you have all the classes a student attempted, you'll need to compute the per-class as sample described. If you want to apply that in the first query, do so.
Once you have that, then, you can roll-up the total credit hrs attempted vs credit hrs earned.
Basic math should help you along.
I didn't quite understand how the GPA is calculated, think there is something wrong with your original post. Here's a starting point that may or may not be right:
SELECT avg(t.grade)
FROM takes t
INNER JOIN course c
ON t.course_id = c.course_id
WHERE t.ID = 1;
SELECT c.dept_name, avg(grade)
FROM takes t
INNER JOIN course c
ON t.course_id = c.course_id
GROUP BY c.dept_name

Find all square grids with fewer crashes in 2007 than in 1989?

My nycgrid table with the following schema id,x1,x2,y1,y2. And it looks like following examples:
22,910000,920000,120000,130000
67,930000,940000,170000,180000
171,980000,990000,210000,220000
Another table, nyccrash, contains tuples with information about the car acciddents that occured from 1989 through 2007 (12 thousand records in total). The first attribute is crash_year, followed by type of accident, weather condition...etc and ending in x_coordinate and y_coordinate where the car crash has occurred in NYC.
2007,2,9,4,1,1028977,202232
2004,1,1,1,4,1012600,214101
2003,1,9,1,1,958775,156149
1999,1,1,1,1,997349,175503
This is an extension question to the one I've asked earlier on stackoverflow.
I am trying to find the square grids (Grid ID) such that they have
less car crashes occurred in 2007 than in 1989. There are about 100
rows in the nycgrid table. I need only those rows that have had less
accidents happened in the year in 2007 than in the year 1989.
And second part, which is probably slightly easier is how could I find
the number of car crashes per each square grid (nycgrid.id)? In other
words, how display how many crashes occurred on each grid id? Each
crash has associated x and y coordinate. Each grid has a x1-x2-y1-y2
coordinates that make up a square.
Starting with RBarryYoung's answer to your last question we just pivot on the year using SUM/CASE and then compare the values
SELECT ID
FROM
(SELECT
grid.ID,
SUM(CASE WHEN yearCol = 1989 THEN 1 ELSE 0 END) CrashCount_1989,
SUM(CASE WHEN yearCol = 2007 THEN 1 ELSE 0 END) CrashCount_2007
FROM crashes
INNER JOIN grid
ON crashes.x_coordinate BETWEEN grid.x1 AND grid.x2
And crashes.y_coordinate BETWEEN grid.y1 AND grid.y2
WHERE crashes.yearCol IN(1989, 2007)
GROUP BY grid.ID) t
WHERE CrashCount_2007 < CrashCount_1989