I would like to create a column in table "starting_pitcher_game_log" for "HomeAway_ID" conditional on values of two columns in another table, "FLD_team_ID" and "Away_Team_ID" such that when "FLD_team_ID" and "Away_Team_ID" are equal to each other, the value for the new column should be "Away" and when they are not equal to each other, the value for the new column should be "Home". The new column's values should provide game-site information ("Home" or "Away") for the "Starting_Pitcher" column in the current table "starting_pitcher_game_log". The "FLD_team_ID" column includes values for the team that the starting pitcher identified in column "Starting_Pitcher" plays for. So, in other words I'd like each row of the "Starting_Pitcher" column, which corresponds to a different game that a given starting pitcher played in, to designate the site of the game, "Home" or "Away", of that game.
Here is the code for the query that goes through, but all but two values for that column are "Home" even though half should be "Home" and half should be "Away"
ALTER TABLE retrosheet.starting_pitcher_game_log ADD HomeAway_ID VARCHAR (4);
UPDATE retrosheet.starting_pitcher_game_log as b, retrosheet.events as g
SET b.HomeAway_ID = IF(g.`AWAY_TEAM_ID`=g.`FLD_TEAM_ID`,"Away","Home")
WHERE b.`Starting_Pitcher` = g.`PIT_ID`
Can someone offer some guidance as to what might be wrong with code that is causing this problem?
UPDATE: Here's the code for the TRIM commands for the four fields in the original code above
UPDATE events SET PIT_ID = TRIM(PIT_ID)
UPDATE starting_pitcher_game_log SET Starting_Pitcher = TRIM(Starting_Pitcher)
UPDATE events SET AWAY_TEAM_ID = TRIM(AWAY_TEAM_ID)
UPDATE events SET FLD_TEAM_ID = TRIM(FLD_TEAM_ID)
Then I ran the following code trying it both with CHAR and VARCHAR for variable type for HomeAway_ID, as well as trying it it with 'AWAY' and 'Home' and then "AWAY" and "Home" in the conditional statement in line 3.
ALTER TABLE retrosheet.starting_pitcher_game_log ADD COLUMN HomeAway_ID CHAR(4);
UPDATE retrosheet.starting_pitcher_game_log AS b, retrosheet.events AS g
SET b.HomeAway_ID=IF(g.`AWAY_TEAM_ID`=g.`FLD_TEAM_ID`,'Away','Home')
WHERE b.`Starting_Pitcher`=g.`PIT_ID`
I'm stumped -- feel like it shouldn't be this hard...It just keeps processing with no end.
Any help with this would be much appreciated.
UPDATE:
I tried the following code, implementing the INNER JOIN command recommended, but it is still taking forever to process query and have yet to see it complete:
ALTER TABLE retrosheet.starting_pitcher_game_log ADD COLUMN HomeAway_ID CHAR (4);
UPDATE retrosheet.starting_pitcher_game_log AS b, retrosheet.events AS g INNER JOIN retrosheet.starting_pitcher_game_log ON g.AWAY_TEAM_ID = g.FLD_TEAM_ID
SET b.HomeAway_ID=IF(g.AWAY_TEAM_ID=g.FLD_TEAM_ID,'Away','Home') WHERE b.Starting_Pitcher=g.PIT_ID
UPDATE:
ALTER TABLE events ADD INDEX (PIT_ID, FLD_TEAM_ID, AWAY_TEAM_ID)
I didn't create a primary key using any of the referenced fields in the events table since I plan to sort those columns referenced in the query in different ways in the future.
Then I ran this query to create the column:
ALTER TABLE retrosheet.starting_pitcher_game_log ADD COLUMN HomeAway_CD CHAR (4);
UPDATE retrosheet.starting_pitcher_game_log AS b, retrosheet.events AS g JOIN retrosheet.starting_pitcher_game_log ON g.AWAY_TEAM_ID = g.FLD_TEAM_ID
SET b.HomeAway_CD=IF(g.AWAY_TEAM_ID=g.FLD_TEAM_ID,'Away','Home') WHERE b.Starting_Pitcher = g.PIT_ID
It just keeps going on forever...
What is the best way to index the columns to accommodate the execution of the query in a somewhat timely fashion.
CORRECT CODE: First I indexed the minimum number of fields in the table (in which I wanted to add the new column HomeAway_CD) that could accurately/sufficiently produce that new column:
ALTER TABLE starting_pitcher_game_log ADD INDEX (GAME_ID, Starting_Pitcher);
Here is the code that worked to produce the column. In the where clause, I had tried "b.GAME_ID=g.GAME_ID" by itself and "b.Starting_Pitcher = g.PIT_ID" by itself, and the query took forever each time. But, both the starting pitcher column and the Game_ID column must be linked from the origin table to the destination table:
UPDATE retrosheet.starting_pitcher_game_log AS b,
retrosheet.events AS g
SET b.HomeAway_CD=IF(g.AWAY_TEAM_ID=g.FLD_TEAM_ID,'Away','Home')
WHERE b.GAME_ID=g.GAME_ID
AND b.Starting_Pitcher = g.PIT_ID