12 Replies Latest reply: Jun 23, 2017 12:31 PM by Wai Meng Leong

# Calculating  and grouping values from two identical column

Hi all,

Below is a sample table of my data and I would like to an overall table where it calculates overall number of wins, overall number of draws and overall number of losses for each team (note that the list in HomeTeam and AwayTeam are the same)

 HomeTeam AwayTeam FTHG FTAG FTR Burnley Swansea 0 1 A Crystal Palace West Brom 0 1 A Everton Tottenham 1 1 D Hull Leicester 2 1 H Man City Sunderland 2 1 H Middlesbrough Stoke 1 1 D Southampton Watford 1 1 D Arsenal Liverpool 3 4 A Bournemouth Man United 1 3 A Chelsea West Ham 2 1 H Man United Southampton 2 0 H Burnley Liverpool 2 0 H Leicester Arsenal 0 0 D Stoke Man City 1 4 A Swansea Hull 0 2 A Tottenham Crystal Palace 1 0 H Watford Chelsea 1 2 A West Brom Everton 1 2 A Sunderland Middlesbrough 1 2 A West Ham Bournemouth 1 0 H

What i have done

Picking the values separately from HomeTeam and AwayTeam is simple. What i am unable to do is to combine the records for HomeTeam and AwayTeam so that we have just 20 teams and the record will show for example:

Team | Played | W | D | L |

Burnley | 2 | 1 | 0 | 1 |

Swansea | 2 | 0 | 0 | 2 |

....

Any help is much appreciated!

Regards,
Wai Meng

• ###### Re: Calculating  and grouping values from two identical column

Data:

AwayTeam,

FTHG,

FTAG,

FTR

FROM

(html, codepage is 1252, embedded labels, table is @1);

New:

FTHG,

FTAG,

FTR,

'HomeTeam' as Flag

Resident Data;

Concatenate(New)

FTHG,

FTAG,

FTR,

'AwayTeam' as Flag

Resident Data;

DROP Table Data;

Create straight table

Dimension:

Team

Expression:

=Count(Team) -> Played

=Count({<FTR={'H'}>}Team) ->W

=Count({<FTR={'A'}>}Team)  ->L

=Count({<FTR={'D'}>}Team)  ->D

• ###### Re: Calculating  and grouping values from two identical column

Thanks Kushal and Vineeth for helping out.

Unfortunately, i gave it a go and Kushal, i think there is a slight error somewhere!

Based on your script, each team still counts on H as a win when in fact, in some games where the team plays away, FTR ='A' should be credited as a win for the team.

Right now, a WIN for a team is still measured only by FTR='H'.

Sorry for the bother!

• ###### Re: Calculating  and grouping values from two identical column

So as per your logic for Swansea, there should 1 win and 1 loose, right?

• ###### Re: Calculating  and grouping values from two identical column

Yes correct.

(Strangely, if you look at your table, it's correct for Arsenal.)

• ###### Re: Calculating  and grouping values from two identical column

Data:

AwayTeam,

FTHG,

FTAG,

FTR

FROM

(html, codepage is 1252, embedded labels, table is @1);

New:

FTHG,

FTAG,

FTR,

'HomeTeam' as Flag

Resident Data;

Concatenate(New)

FTHG,

FTAG,

FTR,

'AwayTeam' as Flag

Resident Data;

DROP Table Data;

Final:

NoConcatenate

if(Flag='HomeTeam' and FTR='H','W',

if(Flag='AwayTeam' and FTR='A','W',

if(FTR='D','D','L'))) as WLD

Resident New;

DROP Table New;

Create straight table

Dimension:

Team

Expression:

=Count(Team) -> Played

=Count({<WLD={'W'}>}Team) ->W

=Count({<WLD={'L'}>}Team)  ->L

=Count({<WLD={'D'}>}Team)  ->D

• ###### Re: Calculating  and grouping values from two identical column

Hey Kushal,

It works now. Great! Thanks so much for your help!

• ###### Re: Calculating  and grouping values from two identical column

I'd suggest a slight change as the total counts are coming incorrect , total matches played in the league should be 20 not 40.

GAME_FACT:

HomeTeam,

AwayTeam,

FTHG,

FTAG,

FTR

FROM

(html, codepage is 1252, embedded labels, table is @1);

TeamsTab:

HomeTeam as TEAM,

'Home' as GameFlag ,

Pick(Match(FTR,'A','H','D'),'L','W','D') as MatchResult

resident GAME_FACT;

Concatenate

AwayTeam as TEAM,

'Away' as GameFlag,

Pick(Match(FTR,'A','H','D'),'W','L','D') as MatchResult

resident GAME_FACT;

Check the app for expressions

• ###### Re: Calculating  and grouping values from two identical column

Have you tried my approach?

• ###### Re: Calculating  and grouping values from two identical column

Hi Vineeth,

Thanks for your approach. It will probably work but it's not feasible in the long run as the games run up to 38 games for each team and there are several league seasons to add to the data. I appreciate your help though, thank you!

• ###### Re: Calculating  and grouping values from two identical column

the only difference is the way you count the Games Played, instead of count(TEAM) , you really need to be counting distinct games played which is why you need a MatchID.

• ###### Re: Calculating  and grouping values from two identical column

Thanks alot for this Vineeth!

• ###### Re: Calculating  and grouping values from two identical column

if you need a more detailed breakdown like bifurcating away and home , wins and loses, try as below

HOME_GAMES:

HomeTeam,FTHG,FTRH

Burnley,0,A

Crystal Palace,0,A

Everton,1,D

Hull,2,H

Man City,2,H

Middlesbrough,1,D

Southampton,1,D

Arsenal,3,A

Bournemouth,1,A

Chelsea,2,H

Man United,2,H

Burnley,2,H

Leicester,0,D

Stoke,1,A

Swansea,0,A

Tottenham,1,H

Watford,1,A

West Brom,1,A

Sunderland,1,A

West Ham,1,H

];

AWAY_GAMES:

AwayTeam,FTAG, FTRA

Swansea,1, A

West Brom,1, A

Tottenham,1, D

Leicester,1, H

Sunderland,1, H

Stoke,1, D

Watford,1, D

Liverpool,4, A

Man United,3, A

West Ham,1, H

Southampton,0, H

Liverpool,0, H

Arsenal,0, D

Man City,4, A

Hull,2, A

Crystal Palace,0, H

Chelsea,2, A

Everton,2, A

Middlesbrough,2, A

Bournemouth,0, H

];

ALL_TEAMS:

HomeTeam as HomeTeam,

HomeTeam as AwayTeam

RESIDENT HOME_GAMES;

Home Wins

count({<FTRH={'A'}>}FTRH)

Home Draws

count({<FTRH={'D'}>}FTRH)

Home Loses

count({<FTRH={'H'}>}FTRH)

Away Wins

count({<FTRA={'A'}>}FTRA)

Away Draws

count({<FTRA={'D'}>}FTRA)

Away Loses

count({<FTRA={'H'}>}FTRA)

Total Wins

rangesum(count({<FTRH={'A'}>}FTRH),count({<FTRA={'A'}>}FTRA))

Total Draws

rangesum(count({<FTRH={'D'}>}FTRH),count({<FTRA={'D'}>}FTRA))

Total Losses

rangesum(count({<FTRH={'H'}>}FTRH),count({<FTRA={'H'}>}FTRA))