I am new to SQL and this is probably fairly easy but I cannot figure out how to make it work.
I am trying to fill a column with data pulled from another column in the same table.. in this case its a basketball database with box scores and I am trying to fill the column of opponent points (opp_pts) to match what their opponent for that game scored. each game is matched by season_id and game_id.
the whole table is about 700 rows with a few hundred games and about 40 teams but a sample is below... this is an example of one game where the score was 84-81 but I want to fill opp_team_stats with the appropriate score
season_id game_id team_id team_pts opp_team_pts
U2018 140 U2018_19 84.0
U2018 140 U2018_23 81.0
I have tried but have only been able to fill the whole column of opp_team_pts with with 84 which is obviously incorrect
UPDATE box_scores
SET opp_team_pts = (SELECT box_scores.team_pts
FROM box_scores
WHERE box_scores.season_id=box_scores.season_id AND box_scores.game_id=box_scores.game_id);
I'm sure the code is probably redundant but that is as far as I got, I understand why it filled the way it did but can't seem to figure out how to fix it... I may be on the wrong track but hopefully can get a bit of help
Assuming that each game has exactly two teams, you can use a correlated subquery:
UPDATE box_scores
SET opp_team_pts = (SELECT bs2.team_pts
FROM box_scores bs2
WHERE bs2.season_id = box_scores.season_id AND
bs2.game_id = box_scores.game_id AND
bs2.team_id <> box_scores.team_id
);
SQLite does not support FROM in the UPDATE statement.
Related
i am struggling to find a solution for following. It is hard to find a title for it by the way :)
I am making a tool where i want to track subscriptions to an event. For managing that I have a table with x (25+) number of positions to be filled.
This tool is in VB.net with an underlying MSSQL Database
That Position will be assigend with a 'userid' and some attributes as 'getradio1' , 'getsradio2' etc.
All of that is easy. So you will get something like this in the 'position' table.
Now it comes - and the questions is twofold : Can it be done and if yes how ?
Every UserID has a kind of priority ranking (in the UserID Database)
Now what i want to is to have the position filled in by order of that ranking . Lets assume that the ranking is as follows
UserID101 = Ranking 14
UserID103 = Ranking 5
UserID106 = Ranking 11
UserID102 = Ranking 39
UserID118 = Ranking 1
UserID114 = Ranking 6
Then i want the table updated so that the position is 'reassigned' according to rank as follows (also including the 'getradio' colums
Ideally would be that if a new PositionID was assigned it would automatically do the 'reordering'.
I tried to descridbe the problem as simple and complete as possible. But if you have more question donot hesitate.
Thanks already for your help
im analyzing some e-sports soccer championship data.
My original table looks like this:
Every row corresponds to one match with the Date, Players envolved, the Teams they used and their Scores
my df head()
After seaching around tableau community, I pivoted "Player A" and "Player B" columns so i can filter for players individually. Now any match has 2 rows(one for each player on that match) and tey're unified by the 'MatchID' column:
my tableau table
That said, i want to build a view where the viewer could select two players and see statistics about all the matches they played against each other, like these two:
1- Last 10 matches info (Date, teams they played with, scores)
2- Most-frequent results like this graph:
the graph i want to show
Tried bringing some dimensions to colums but i really couldnt find a way to show the entire row data in a view. No idea about h2 filter from two players and take only matches where they encounter using MatchID.
I tried searching around and do some Calculated Fields filters, but i just went Tableau with no background in SQL, Excel or anything, just Python. So im a bit lost with so many options and ways.
If anyone could gimme directions about that i would be very happy. Thx in advice (:
I think you should unpivot your data so you are back with 1 record per match. Then you will be able to use 2 parameters as your filters; one parameter for player 1 and the other for player 2. That would enable the user to select 2 different players.
As there's a chance the same player could be in both the Player 1 and Player 2 columns, to use as a filter is a little more complex. Your filter calculated field for the Player1 parameter would be something like:
[FilterParameterPlayer1]: [ParameterPlayer1] = [Player1] OR ParameterPlayer1] = [Player2]
And for Player2 parameter:
[FilterParameterPlayer2]: [ParameterPlayer2] = [Player1] OR ParameterPlayer2] = [Player2]
Both filter fields should be set to only show True.
I am looking for a way to search for a certain number of rows as a quality check. For example, we have tables that have a certain set of results that are needed.
Here is a quick table for an example:
ID: Name: Result: Reportable:
ONE A 10 X
TWO B 12 X
THREE C 1
FOUR D 18 X
FOUR(redo) D 11 X
So we are looking to double check results as there are people who accidentally report results multiple times (as in the case with ID FOUR). We have used having counts but we need the numbers to be specific and need a query to verify that number is satisfied.
In the table above we only want IDs ONE, TWO, and FOUR, however we have 4 results (one extra). Currently we have our check showing the count needed (ie 3) and the current result count (4) to show the mismatch but want a query to easily only show the result needed. We would need the redo result most of the time so we have set it so we take the latest date, but it doesn't help filter how many rows or results. I apologize if anything is confusing and I am not able to share the SQL query that we have currently. It's my first time posting so if I need to clarify anything please let me know as this seems to be very complicated. Thank you for your time.
EDIT: The details
We have one table (Table A) letting us know which results are reportable. The ones that are reportable go into another table (Table B). We have had issues in which people have made too many results reportable which overpopulates the Table B. Our old query had a count in Table B, but due to mistakes in people placing multiple reportables, samples which had many redos seem to be finished as they were all placed and met the count in Table B.
So now by using the Table A that helps tell us how many are Reportable, we want this to double check that the samples are indeed ready.
As I understand the question, you want ids that have multiple reportables. Assuming you really mean name, then:
select name
from t
where reportable = 'X'
group by name
having count(*) >= 2;
I've created a procedure that predicts College football game lines by using the variables #Team1 and #Team2. In the current setup, these teams are entered manually.
For example: #Team1 = 'Ohio St.', #Team2 = 'Southern Miss.'
Then, my calculation will go through a series of calculations on stats comparisons, strength of schedule, etc. to calculate the hypothetical game line (in this case, Ohio St. -39.)
Here's where I need your help: I'm trying to turn this line prediction system into a ranking system, ranking each team from greatest to worst. I'd like to take each team in my Team table and put it through this calculation with each possible matchup. Then, rank the teams based on who has the biggest advantage over every team that given week, vs. who has the least advantage.
Any ideas? I've toyed around with the idea of turning the calculation into a function and pass the values through that way, but not sure where to start.
Thanks!
Apologies for the made-up column names, but the following should do what you want if you convert your proc to a function that takes the two team names as arguments:
Select a.Name as Team1
, b.Name as Team2
, fn_GetStats(a.Name, b.Name)
from TeamsList a
inner join TeamsList b
on a.Name > b.Name --To avoid duplicate rows
order by 3 desc
The join will create a list of all possible unique combinations (e.g. TeamB and TeamA, but not also TeamA and TeamB or TeamA and TeamA).
Assuming the proc outputs just a single value right now, this seems like the easiest solution. You could also do the same join and then loop through your proc with the results, instead.
I have 2 tables in SQL : Event and Swimstyle
The Event table has a value SwimstyleId which refers to Swimstyle.id
The Swimstyle table has 3 values : distance, relaycount and strokeid
Normally there would be somewhere between 30 and 50 rows in the table Swimstyle, which would hold all possible values (these are swimming distances like 50 (distance), 1 (relaycount), FREE (strokeid)).
However, due to a programming mistake the lookup for existing values didn't work and the importer of new results created a new swimstyle entry for each event added...
My Swimstyle table now consists of almost 200k rows, which ofcourse is performance wise not the best idea ;)
To fix this i want to go through all Events, get the swimstyle values that are attached, lookup the first existing row in Swimstyle that has the same distance, relaycount and strokeid values and update the Event.SwimstyleId with that value.
When this is all done i can delete all orphaned Swimstyle rows, leaving a table with only 30-50 rows.
I have been trying to make a query that does this, but not getting anywhere. Anyone to point me in the right direction ?
These 2 statements should fix the problem, if I've read it right. N.B. I haven't been able to try this out anywhere, and I've made a few assumptions about the table structure.
UPDATE event e
set swimstyle_id = (SELECT MIN(s_min.id)
FROM swimstyle s_min,swimstyle s_cur
WHERE s_min.distance = s_cur.distance
AND s_min.relaycount = s_cur.relaycount
AND s_min.strokeid = s_cur.strokeid
AND s_cur.id = e.swimstyle_id);
DELETE FROM swimstyle s
WHERE NOT EXISTS (SELECT 1
FROM event e
WHERE e.swimstyle_id = s.id);