Jump to content

[SOLVED] SUM output into table


nikkicade

Recommended Posts

Hi

 

I have a table with reults and points in. I want to add all the points and then insert the total in a column called TotalPts. I can get the total worked out using the following:-

 

Select Username, sum(FormationPts+GoalkeeperPts+RDefenderPts+CDefender1Pts+CDefender2Pts+CDefender3Pts+LDefenderPts+RMidfieldPts+CMidfield1Pts+CMidfield2Pts+CMidfield3Pts+LMidfieldPts+Striker1Pts+Striker2Pts+Striker3Pts+TacticalSubPts+Sub1Pts+Sub2Pts+Sub3Pts+Sub4Pts+Sub5Pts+ResultsPts+HalfTimeLPts+FullTimeLPts+Scorer1Pts+Card1Pts+GatePts+ShotsTargetPts+CornersPts+PossessionPts+TimeGoalPts+TotalYellowPts+TotalRedPts+TotalFoulsPts+Foul1Pts+TimeofCardPts+TimeofSubPts) from game20sep group by Username;

 

This displays the username and TotalPts for each user but rather than me editing the TotalPts column for each user is there a simple way of inserting the TotalPts for each user?

 

I have tried:-

 

Update game20sep set TotalPts=sum(FormationPts+GoalkeeperPts+RDefenderPts+CDefender1Pts+CDefender2Pts+CDefender3Pts+LDefenderPts+RMidfieldPts+CMidfield1Pts+CMidfield2Pts+CMidfield3Pts+LMidfieldPts+Striker1Pts+Striker2Pts+Striker3Pts+TacticalSubPts+Sub1Pts+Sub2Pts+Sub3Pts+Sub4Pts+Sub5Pts+ResultsPts+HalfTimeLPts+FullTimeLPts+Scorer1Pts+Card1Pts+GatePts+ShotsTargetPts+CornersPts+PossessionPts+TimeGoalPts+TotalYellowPts+TotalRedPts+TotalFoulsPts+Foul1Pts+TimeofCardPts+TimeofSubPts) group by Username;

 

but this comes back with 'error in your SQL syntax'.

 

mySql version 5.0.19

 

Does anyone have any ideas?

 

Thanks

Link to comment
Share on other sites

You could just do a subquery, or better yet a join.

 

UPDATE game20sep g1 JOIN game20sep g2 ON g2.Username = g1.Username SET TotalPts = SUM(<all of your columns);

 

 

 

Test that before you run it on live data though, as I could have the logic wrong or a typo.

Link to comment
Share on other sites

Hi Corbin

 

Tried your code but came back with "Column 'TotalPts' in field list is ambiguous". Not used JOIN before but tried adding g1. before TotalPts and it came back with "Invalid use of Group function". The Username is in the same table as the results and points so do I need Join as I thought this linked or joined tables?

 

All I need to do is write the results of the sum into the TotalPts column but do it by table row as the Username is unique and therefore there is only 1 row per username. If you use:-

 

insert into game20sep (TotalPts) Value (SUM(FormationPts+GoalkeeperPts+RDefenderPts+CDefender1Pts+CDefender2Pts+CDefender3Pts+LDefenderPts+RMidfieldPts+CMidfield1Pts+CMidfield2Pts+CMidfield3Pts+LMidfieldPts+Striker1Pts+Striker2Pts+Striker3Pts+TacticalSubPts+Sub1Pts+Sub2Pts+Sub3Pts+Sub4Pts+Sub5Pts+ResultsPts+HalfTimeLPts+FullTimeLPts+Scorer1Pts+Card1Pts+GatePts+ShotsTargetPts+CornersPts+PossessionPts+TimeGoalPts+TotalYellowPts+TotalRedPts+TotalFoulsPts+Foul1Pts+TimeofCardPts+TimeofSubPts)) on duplicate key update TotalPts=SUM(FormationPts+GoalkeeperPts+RDefenderPts+CDefender1Pts+CDefender2Pts+CDefender3Pts+LDefenderPts+RMidfieldPts+CMidfield1Pts+CMidfield2Pts+CMidfield3Pts+LMidfieldPts+Striker1Pts+Striker2Pts+Striker3Pts+TacticalSubPts+Sub1Pts+Sub2Pts+Sub3Pts+Sub4Pts+Sub5Pts+ResultsPts+HalfTimeLPts+FullTimeLPts+Scorer1Pts+Card1Pts+GatePts+ShotsTargetPts+CornersPts+PossessionPts+TimeGoalPts+TotalYellowPts+TotalRedPts+TotalFoulsPts+Foul1Pts+TimeofCardPts+TimeofSubPts);

 

you get an error "Invalid use of Group function" similar to the update command in first post and if you add 'GROUP BY Username' on the end it says there is an error in the syntax.

 

I am lost with this as I can get the Sum to work but can't seem to write it into the column in the table.

 

Thanks

 

Nikki

Link to comment
Share on other sites

Hi Corbin

 

I tried that but it said Invalid use of group function

 

 

Tried this :-

 

select sum(FormationPts+GoalkeeperPts+RDefenderPts+CDefender1Pts+CDefender2Pts+CDefender3Pts+LDefenderPts+RMidfieldPts+CMidfield1Pts+CMidfield2Pts+CMidfield3Pts+LMidfieldPts+Striker1Pts+Striker2Pts+Striker3Pts+TacticalSubPts+Sub1Pts+Sub2Pts+Sub3Pts+Sub4Pts+Sub5Pts+ResultsPts+HalfTimeLPts+FullTimeLPts+Scorer1Pts+Card1Pts+GatePts+ShotsTargetPts+CornersPts+PossessionPts+TimeGoalPts+TotalYellowPts+TotalRedPts+TotalFoulsPts+Foul1Pts+TimeofCardPts+TimeofSubPts) AS TotalPts from game20sep group by Username;

 

and it shows the total In the TotalPts column but it still does not write it to the column.

Link to comment
Share on other sites

Oh, that's right.... It would need to be grouped, and grouping can't be done in an update query as far as I know.

 

 

 

I guess a subquery would be one way.

 

 

UPDATE game20sep g1 SET TotalPts = (SELECT SUM(your columns) FROM game20sep g2 WHERE g2.username = g1.username GROUP BY g2.username);

 

 

That might invalid syntax.  Haven't done a subquery in a while.

 

 

Also, you'll probably need to do SUM(col1) + sum(col2) not lump them all in 1 sum.

 

 

 

 

By the way, I think your table structure could be optimized to not run into this problem.  Your table structure sounds kind of bad based on the query you used as an example.

Link to comment
Share on other sites

Tried that and got an error "You can't specify target table 'g1' for update in FROM clause".

 

As I have a similar problem with the monthly totals and running totals for the year - maybe it would be better to create a totals table and have a TotalPts for each game as well as a monthly total and a running total and then use something like

 

Update totalsable g1 set Game1TotalPts=(SELECT SUM(your columns) FROM game20sep g2 WHERE g2.username = g1.username GROUP BY g3.username);

 

Do you think this would be better?

 

Link to comment
Share on other sites

Just tried it with a new table called alltotals. Used this :-

 

update alltotals g1 SET GameTotal= (SELECT SUM(FormationPts+GoalkeeperPts+RDefenderPts+CDefender1Pts+CDefender2Pts+CDefender3Pts+LDefenderPts+RMidfieldPts+CMidfield1Pts+CMidfield2Pts+CMidfield3Pts+LMidfieldPts+Striker1Pts+Striker2Pts+Striker3Pts+TacticalSubPts+Sub1Pts+Sub2Pts+Sub3Pts+Sub4Pts+Sub5Pts+ResultsPts+HalfTimeLPts+FullTimeLPts+Scorer1Pts+Card1Pts+GatePts+ShotsTargetPts+CornersPts+PossessionPts+TimeGoalPts+TotalYellowPts+TotalRedPts+TotalFoulsPts+Foul1Pts+TimeofCardPts+TimeofSubPts) FROM game20sep g2 WHERE g2.username = g1.username GROUP BY g2.username);

 

and it worked a treat :-)

 

Thanks again for your help

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.