LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   SQL Query question (https://www.linuxquestions.org/questions/programming-9/sql-query-question-135275/)

oulevon 01-15-2004 09:38 PM

SQL Query question
 
Hi,

I'm not that familiar with sql. Here's my situation. I'm running mysql, and I have a table of every Batter from the 2003 Baseball season. Each record contains playerID, teamID, games, hits etc. The problem is that some players were traded in midseason last year so there are 2 records for those players, one for their stats from team 1 and one from their stats from team 2. Is there a way in SQL to combine two records? All the fields I need to combine would be small integers.

Alternatively I could combine them after extracting them from the database, but I was just wondering if I could do it through SQL. Thanks for any help.

lyle_s 01-15-2004 11:51 PM

You mean the playerID is different even though it's the same player on a different team?

Lyle

oulevon 01-16-2004 12:17 AM

No I mean the following:

Code:


+-----------+--------+------+------+------+
| playerID  | teamID | G    | H    | SB  |
+-----------+--------+------+------+------+
| sanchal03 | MIL    |  43 |  46 |    8 |
| sanchal03 | DET    |  101 |  114 |  44 |
+-----------+--------+------+------+------+
2 rows in set (0.00 sec)


So the player is the same, and the playerID is the same but there are 2 different entries one for each team. So I want something like the following:

sanchal03 | DET | 144 | 160 | 52 |

lyle_s 01-16-2004 12:28 AM

Oh, okay. Hopefully this will help:

Code:

SELECT playerID, SUM(G), SUM(H), SUM(SB)
FROM tablename
WHERE playerID='sanchal03';

Lyle

oulevon 01-16-2004 12:41 AM

Thanks for your help, but I'm getting the following error:

Code:

mysql> SELECT playerID, SUM(G), SUM(H), SUM(SB) FROM Batting WHERE playerID='sanchal03';
ERROR 1140: Mixing of GROUP columns (MIN(),MAX(),COUNT()...) with no GROUP columns is illegal if there is no GROUP BY clause


I'm not sure if this will help, but here is how the Table is setup:

Code:

mysql> DESCRIBE Batting;
+----------+----------------------+------+-----+---------+-------+
| Field    | Type                | Null | Key | Default | Extra |
+----------+----------------------+------+-----+---------+-------+
| playerID | varchar(9)          |      | PRI |        |      |
| yearID  | smallint(4) unsigned |      | PRI | 0      |      |
| stint    | int(2)              |      | PRI | 0      |      |
| teamID  | char(3)              |      | MUL |        |      |
| lgID    | char(2)              |      |    |        |      |
| G        | smallint(3) unsigned | YES  |    | NULL    |      |
| AB      | smallint(3)          |      |    | 0      |      |
| R        | smallint(3) unsigned | YES  |    | NULL    |      |
| H        | smallint(3) unsigned | YES  |    | NULL    |      |
| 2B      | smallint(3) unsigned | YES  |    | NULL    |      |
| 3B      | smallint(3) unsigned | YES  |    | NULL    |      |
| HR      | smallint(3) unsigned |      |    | 0      |      |
| RBI      | smallint(3) unsigned | YES  |    | NULL    |      |
| SB      | smallint(3) unsigned | YES  |    | NULL    |      |
| CS      | smallint(3) unsigned | YES  |    | NULL    |      |
| BB      | smallint(3) unsigned | YES  |    | NULL    |      |
| SO      | smallint(3) unsigned | YES  |    | NULL    |      |
| IBB      | smallint(3) unsigned | YES  |    | NULL    |      |
| HBP      | smallint(3) unsigned | YES  |    | NULL    |      |
| SH      | smallint(3) unsigned | YES  |    | NULL    |      |
| SF      | smallint(3) unsigned | YES  |    | NULL    |      |
| GIDP    | smallint(3) unsigned | YES  |    | NULL    |      |
+----------+----------------------+------+-----+---------+-------+
22 rows in set (0.00 sec)


nephilim 01-16-2004 01:03 AM

I think that should be

Code:

SELECT playerID, SUM(G), SUM(H), SUM(SB)
FROM tablename
WHERE playerID='sanchal03'
GROUP BY playerID;


oulevon 01-16-2004 01:13 AM

That works great! Thank you both for helping me out.

nephilim 01-16-2004 01:50 AM

No problem, anytime.


All times are GMT -5. The time now is 03:04 PM.