Summarizing Data Within SQL - mysql

Hi everyone I am new hear and I'm stuck summarizing within my SQL Database.
To make the example very simple here is my data below.
GAME_ID GOALIEID SCORED
2001 5 N
2001 4 N
2001 5 Y
2001 4 N
2001 5 Y
This is a shootout and I want to summarize the amount of saves each goalie made by Game_ID, GoalieID, and how many saves were actually made by each.
The N=SAVE AND Y=GOAL GIVEN UP
I am trying to output my result as shown below in descending order:
GAME_ID GOALIEID SAVES
2001 4 2
2001 5 1
Currently based on my code:
SELECT GAME_ID, GOALIEID, COUNT(SCORED) AS SAVES
FROM GIRAFFE.MLS
WHERE SCORED = 'N'
GROUP BY GAME_ID
ORDER BY COUNT (SCORED) DESC
But the result I am getting is just adding up all saves and attributing it to one goalie as show below.
GAME_ID GOALIEID SAVES
2001 5 3
THE ABOVE WAS RESOLVED.
The FINAL QUESTION TO FINALIZE THIs is how do you query for a STREAK in SQL
For example:
GAME_ID GOALIEID TEAMID TEAMSHOTNUM OVERALLSHOT SCORED
2001 5 1 1 1 Y
2001 4 2 1 2 N
2001 5 1 2 3 N
2001 4 2 2 4 N
2001 5 1 3 5 N
2001 4 2 3 6 N
Based on this simple example:
How can I query for the most consecutive "Saves" which would be "N" by a "GoalieID" now imagine my data has multiple games to look through but for simplicity I only have the one game here.
My result I would hope to look like this:
GAME_ID GOALIEID STREAK
2001 4 3
This would show that "GOALIEID" #4 made 3 consecutive saves to win the game showing that the highest consecutive streak of saves was in fact 3. Note remember this will be across thousands of games. But i made it simple here with just 1 game.
THANK YOU ALL FOR YOUR HELP!

Actually you should group by GAME_ID AND GOALIEID :
SELECT GAME_ID, GOALIEID, COUNT(SCORED) AS SAVES
FROM a
WHERE SCORED = 'N'
GROUP BY GAME_ID, GOALIEID
ORDER BY SAVES DESC

Related

Find the lowest value of every row of selected columns

As the result of the query, I want to get all rows (Drivers), order by the drivers who got most series wins.
If a driver has won 4 tacks at least one or more times but failed to win the remaining track at least once, his series count is 0.
Driver Table
ID|Name| .........
1 A
2 B
3 C
4 D
Tracks Table
TID |FK|Track1_Wins|Track2_Wins| Track3_Wins|Track4_Wins|Track5_Wins|
1 1 5 6 3 2 4
2 2 2 4 0 5 3
3 3 6 3 9 4 7
4 4 5 8 2 4 1
My code sample
SELECT `Drivers`.`Name`, LEAST(`Track1_Wins`, `Track2_Wins`, `Track3_Wins`, `Track4_Wins`, `TRACK5_Wins`) AS Series
FROM `Drivers`, `Tracks`
ORDER BY Series DESC;
Accidently I got part expected output when I use WHERE with Driver ID
SELECT `Drivers`.`Name`, LEAST(`Track1_Wins`, `Track2_Wins`, `Track3_Wins`, `Track4_Wins`, `TRACK5_Wins`) AS Series FROM `Drivers`, `Tracks` WHERE `Drivers`.`ID` = 2 ORDER BY Series DESC;
It will give the expected result but with Same Driver Name as expected
B 3
B 2
B 1
B 0
My expected output is
Name | Series
C 3
A 2
D 1
B 0
Run this,
SELECT d.`Name`,
LEAST(`Track1_Wins`, `Track2_Wins`, `Track3_Wins`, `Track4_Wins`, `TRACK5_Wins`) AS Series
FROM `Drivers` d INNER JOIN `Tracks` t
ON t.`FK` = d.`ID`
ORDER BY Series DESC;
This returns the user name associated with the FK. Also, try to use kebab_case and lower case for all your column and table name. Makes it much easier to run the code

SQL - Max value from a group by when creating a new field

I have a database with a table called BOOKINGS containing the following values
main-id place-id start-date end-date
1 1 2018-8-1 2018-8-8
2 2 2018-6-6 2018-6-9
3 3 2018-5-5 2018-5-8
4 4 2018-4-4 2018-4-5
5 5 2018-3-3 2018-3-10
5 1 2018-1-1 2018-1-6
4 2 2018-2-1 2018-2-10
3 3 2018-3-1 2018-3-28
2 4 2018-4-1 2018-4-6
1 5 2018-5-1 2018-5-15
1 3 2018-6-1 2018-8-8
1 4 2018-7-1 2018-7-6
1 1 2018-8-1 2018-8-18
1 2 2018-9-1 2018-9-3
1 5 2018-10-1 2018-10-6
2 5 2018-11-1 2018-11-5
2 3 2018-12-1 2018-12-25
2 2 2018-2-2 2018-2-19
2 4 2018-4-4 2018-4-9
2 1 2018-5-5 2018-5-23
What I need to do is for each main-id I need to find the largest total number of days for every place-id. Basically, I need to determine where each main-id has spend the most time.
This information must then be put into a view, so unfortunately I can't use temporary tables.
The query that gets me the closest is
CREATE VIEW `MOSTTIME` (`main-id`,`place-id`,`total`) AS
SELECT `BOOKINGS`.`main-id`, `BOOKINGS`.`place-id`, SUM(DATEDIFF(`end-date`, `begin-date`)) AS `total`
FROM `BOOKINGS`
GROUP BY `BOOKINGS`.`main-id`,`RESERVATION`.`place-id`
Which yields:
main-id place-id total
1 1 24
1 2 18
1 5 5
2 1 2
2 2 20
2 4 9
3 1 68
3 2 24
3 3 30
4 1 5
4 2 10
4 4 1
5 1 19
5 2 4
5 5 7
What I need is then the max total for each distinct main-id:
main-id place-id total
1 1 24
2 2 20
3 1 68
4 2 10
5 1 19
I've dug through a large amount of similar posts that recommend things like self joins; however, due to the fact that I have to create the new field total using an aggregate function (SUM) and another function (DATEDIFF) rather than just querying an existing field, my attempts at implementing those solutions have been unsuccessful.
I am hoping that my query that got me close will only require a small modification to get the correct solution.
Having hyphen character - in column name (which is also minus operator) is a really bad idea. Do consider replacing it with underscore character _.
One possible way is to use Derived Tables. One Derived Table is used to determine the total on a group of main id and place id. Another Derived Table is used to get maximum value out of them based on main id. We can then join back to get only the row corresponding to the maximum value.
CREATE VIEW `MOSTTIME` (`main-id`,`place-id`,`total`) AS
SELECT b1.main_id, b1.place_id, b1.total
FROM
(
SELECT `main-id` AS main_id,
`place-id` AS place_id,
SUM(DATEDIFF(`end-date`, `begin-date`)) AS total
FROM BOOKINGS
GROUP BY main_id, place_id
) AS b1
JOIN
(
SELECT dt.main_id, MAX(dt.total) AS max_total
FROM
(
SELECT `main-id` AS main_id,
`place-id` AS place_id,
SUM(DATEDIFF(`end-date`, `begin-date`)) AS total
FROM BOOKINGS
GROUP BY main_id, place_id
) AS dt
GROUP BY dt.main_id
) AS b2
ON b1.main_id = b2.main_id AND
b1.total = b2.max_total
MySQL 8+ solution would be utilizing the Row_Number() functionality:
CREATE VIEW `MOSTTIME` (`main-id`,`place-id`,`total`) AS
SELECT b.main_id, b.place_id, b.total
FROM
(
SELECT dt.main_id,
dt.place_id,
dt.total
ROW_NUMBER() OVER (PARTITION BY dt.main_id
ORDER BY dt.total DESC) AS row_num
FROM
(
SELECT `main-id` AS main_id,
`place-id` AS place_id,
SUM(DATEDIFF(`end-date`, `begin-date`)) AS total
FROM BOOKINGS
GROUP BY main_id, place_id
) AS dt
GROUP BY dt.main_id
) AS b
WHERE b.row_num = 1

Sum within a column given two or more conditions in MySQL

In MySQL, I am trying to sum values in a column given certain conditions. I have an example of some data below
Team Season Mth Stat
A 1 1 4
A 1 1 4
A 1 2 7
A 1 2 9
B 1 1 6
B 1 1 6
B 1 2 6
B 1 2 9
C 1 1 1
C 1 1 3
C 1 2 3
C 1 2 6
But I need the output to show up as
Team Season Mth Stat
A 1 1 8
A 1 2 16
B 1 1 12
B 1 2 15
C 1 1 4
C 1 2 9
So the Stat column is now the sum of the cells such that Match, Season, and Team are all the same. I have the code below. I see a lot of answers that use 'case' but that seems to be given logical operators that are not equal to each other. When I do it below, now it doesn't recognise the table where the columns are coming from. I do have a inner joins but the data itself is from one table. I get another error as well on the sum function because it requires one argument.
select
Team
,Season
,Match
--this is where I get lost-----------
sum(
select
Stat
From
table
Where
Mth=Mth
AND Season=Season
AND Team=Team
)
--end of getting lost----------------
FROM
table
Where
Season IN (1,2)
GROUP BY
Team
,Season
,Mth
Order BY
Team ASC
Edit:
It turns out I need to use GROUP BY as the comments suggest. So I am not summing within a table, but I sum the variable given the Group By parameters.
Unless I'm missing something, it's simply:
SELECT Team
,Season
,Match
,Sum(Stat)
FROM table
GROUP BY
Team
,Season
,Match
It's simple as this:
SELECT Team,
Season,
Match,
SUM(Stat)
FROM Table
WHERE Season IN (1,2)
GROUP BY Team,
Season,
Match
ORDER BY Team ASC
Please look at the SQL Fiddle example.

Count how many times two values occur in the same row

I am trying to pull a few reports for a client and I'm having trouble nailing one of queries down. I'd like to count the amount of times each customer has visited each location.
Sales Table
CustNo|StoreNo
2 10
2 10
3 5
3 10
2 20
Return Query
CustNo|StoreNo|Count
2 10 2
2 20 1
3 5 1
3 10 1
Thank you in advance for the help!
You need to use group by keyword like:
select CustNo,StoreNo,Count(*) as VisitCount
from Sales
group by CustNo, StoreNo

Trying to sum and group two sets of results from three SQL tables

I am having some major difficulties with grouping and summing results of a query in the way I want. I am having trouble explaining what I want to do in words, so I'll just show you. I have three tables: A, H, and W which look like (simplified):
Table A:
Aid name
1 adam
2 bob
Table H:
Hid Wid date atk Aid
1 1 - 10 2
2 2 - 1 1
3 2 - 5 1
4 1 - 2 2
5 1 - 22 1
6 2 - 7 2
Table W:
Wid name user pass
1 charlie - -
2 donald - -
I am trying to get the SUM of atk grouped by Aid and Wid. Basically, assume this is a fight club tally. I want to display the sum of how many times person W attacked person A (it will always be a one directional fight, ie: charlie can only attack adam, but adam can't attack charlie). (not really a fight club - being used for an online game :))
I am trying to get my result to look like:
name1 atk name2
charlie 22 adam
charlie 12 adam
donald 6 bob
donald 7 bob
My current query looks like...
SELECT w.name AS name1, h.atk, a.name AS name2
FROM H
JOIN W ON w.Wid=h.Wid
JOIN A ON a.Aid=h.Aid
...which gives me every instance that name1 attacked name2. When I try to GROUP BY and/or SUM(h.atk) it is grouping or summing in a way I can't figure out. I'm just not understanding how to accomplish this. Any help would be greatly appreciated.
Thanks in advance!
SELECT w.name AS name1, sum(h.atk) as atk, a.name AS name2
FROM H
JOIN W ON w.Wid=h.Wid
JOIN A ON a.Aid=h.Aid
GROUP BY w.name, a.name