Click here to Skip to main content
15,889,992 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello
The below code works for giving me weekly scores for a team of 4 players.

How do I get a running total?

Have tried few ways without success.


EDIT: for anyone following this thread, here is some test data:

SQL
(
   select
       '01' as Week,
       4 as TeamNum,
       1 as GameNum,
       684 as Gm1,
       810 as Gm2,
       659 as Gm3
   union select
       '02' as Week,
       4 as TeamNum,
       2 as GameNum,
       667 as Gm1,
       810 as Gm2,
       659 as Gm3
   union select
       '03' as Week,
       4 as TeamNum,
       3 as GameNum,
       506 as Gm1,
       810 as Gm2,
       659 as Gm3
   union select
       '04' as Week,
       4 as TeamNum,
       4 as GameNum,
       668 as Gm1,
       810 as Gm2,
       659 as Gm3
   union select
       '05' as Week,
       4 as TeamNum,
       5 as GameNum,
       688 as Gm1,
       810 as Gm2,
       659 as Gm3) as testData


Sorry for confusion but previous answer worked partially and did not notice until today.
It gave the result of a running total but did not separate between teams.
When I tried different versions of grouping by "week" or "team" did not work.
Thanks for solutions but I will give some actual sample data and the expected result for clarifications.

1.
table b_Scores
Week	TeamNum	Gm1	Gm2	Gm3	Total	Won	Lost
01	1	283	263	323	  869	2	1
01	2	798	768	703	2,269	1	2
01	3	599	701	580	1,880	2	1
02	1	446	384	456	1,286	0	3
02	2	748	811	738	2,297	1	2
02	3	550	551	491	1,592	3	0
03	1	405	380	395	1,180	3	0
03	2	683	787	803	2,273	0	3
03	3	564	694	604	1,862	3	0

2.
table b_Matches
MatchWk	MatchTm	WON	LOST
1	1	2	1
1	2	1	2
1	3	2	1
2	1	0	3
2	2	1	2
2	3	3	0
3	1	3	0
3	2	0	3
3	3	3	0

3. Expected Result.
RESULT EXPECTED
Week	Team  Num	Gm1	Gm2	Gm3	Total	Rtotal	Won	Lost	Total  Wins	Total Losses
1	1	283	263	323	  869	  869	2	1	2	1
1	2	798	768	703	2,269	3,138	1	2	3	3
1	3	599	701	580	1,880	5,018	2	1	5	4
2	1	446	384	456	1,286	1286	0	3	0	3
2	2	748	811	738	2,297	3,583	1	2	1	5
2	3	550	551	491	1,592	5,175	3	0	4	5
3	1	405	380	395	1,180	1180	3	0	3	0
3	2	683	787	803	2,273	3,453	0	3	3	3
3	3	564	694	604	1,862	5,315	3	0	6	3

What I have tried:

<pre>SELECT 
    Week, 
    TeamNum , 
    Gm1, 
    Gm2, 
    Gm3, 
    Gm1 + Gm2 + Gm3 AS total, 
    @rt := @rt + Gm1 + Gm2 + Gm3 AS rt 
FROM 
     b_Scores
        
join (SELECT @rt:=0 ) as dummy

This is my code with previous answer included.
(SELECT 'Week', 'TeamNum', 'Gm1', 'Gm2', 'Gm3','Total', 'rt' ,'Won', 'Lost', 'Ttl Wins', 'Ttl Losses')
UNION
( SELECT
 Week,
 TeamNum,
FORMAT( SUM(Gm1),0) AS "Gm1", 
FORMAT( SUM(Gm2),0) AS "Gm2", 
FORMAT( SUM(Gm3),0) AS "Gm3", 
Format(SUM(Gm1)+ SUM(Gm2)+ SUM(Gm3),0) as "Total",
@rt := @rt + Gm1 + Gm2 + Gm3 AS rt , 
  WON,  LOST, 
Format(SUM(won),0) as "TtlWins",
Format(SUM(lost),0) as "TtlLosses" 
FROM b_Scores R 
	JOIN (SELECT @rt:=0 ) as dummy
    JOIN b_Matches 	
    ON Week = MatchWk 	AND TeamNum = MatchTm
/* WHERE Week < 4 and TeamNum < 4 */
GROUP BY TeamNum, Week 
ORDER BY `TeamNum`,`Week`)

Result:
Week	TeamNum	Gm1	Gm2	Gm3	Total	rt	Won	Lost	Ttl Wins	Ttl Losses
1	1	283	263	323	869	418	2	1	4	2
2	1	446	384	456	1,286	5463	0	3	0	9
3	1	405	380	395	1,180	10562	3	0	9	0
1	2	798	768	703	2,269	1459	1	2	4	8
2	2	748	811	738	2,297	6946	1	2	4	8
3	2	683	787	803	2,273	11853	0	3	0	12
1	3	599	701	580	1,880	3516	2	1	8	4
2	3	550	551	491	1,592	8960	3	0	12	0
3	3	564	694	604	1,862	14066	3	0	12	0

In this result had the following errors.
1. rt (running total) did not work when grouped by week and team.
2. Total wins or losses did not calculate properly. Added this portion because had hard time incorporating previous code into my code when i had additional joins.

Hope this additional data helps for clarity.
Posted
Updated 7-Mar-17 18:56pm
v5
Comments
Andy Lanng 7-Mar-17 8:40am    
a few questions.
Is this T-Sql (mssql) or some other sql?
what data do you have and what do you expect.
I would like to know so I can try to write a solution that fits. With t-sql it should be very simple
Member 12995602 7-Mar-17 9:08am    
It is Mysql.
This should make it clearer.
Week TeamNum Gm1 Gm2 Gm3 Total Need This RTtl
01 4 684 810 659 2,153 2,153
02 4 667 714 787 2,168 4,321
03 4 506 543 661 1,710 6,031
04 4 668 670 696 2,034 8,065
05 4 688 660 712 2,060 10,125
Patrice T 7-Mar-17 9:42am    
Use Improve question to update your question.
So that everyone can pay attention to this information.

Try this:
SELECT Week, TeamNum , SUM(Gm1), SUM(Gm2), SUM(Gm3), 
SUM(Gm1 + Gm2 + Gm3) AS total,
(@rt := @rt + SUM(Gm1 + Gm2 + Gm3)) AS rt FROM tablename, (SELECT @rt:=0) as dummy GROUP BY Week
 
Share this answer
 
v2
Comments
Member 12995602 7-Mar-17 9:46am    
I am getting error at row 4.
Forgot to mention that i am using Mysql pre 2012 version.
Peter Leow 7-Mar-17 9:57am    
What error message? What is in row 4? Don't expect us to see your screen.
Member 12995602 7-Mar-17 10:03am    
Thanks for help.
I couldn't get Solution 1 working so I made some tweaks. I hope this is accurate:

SQL
SELECT 
	Week, 
    TeamNum , 
    Gm1, 
    Gm2, 
    Gm3, 
    Gm1 + Gm2 + Gm3 AS total, 
    @rt := @rt + Gm1 + Gm2 + Gm3 AS rt 
FROM 
     [tablename]
        
join (SELECT @rt:=0 ) as dummy 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900