Aggregation

6 views
Skip to first unread message

Michel Martens

unread,
Jul 2, 2011, 4:36:48 PM7/2/11
to band...@googlegroups.com
Hello guys,

First of all, thanks for this very interesting language :-)

I'm playing with bandicoot and given the following:

rel Seasons {
year: int,
league: string,
team: string,
points: int,
}

with this data:

year:int,league:string,team:string,points:int
2011,A,Vélez Sarsfield,39
2011,A,Lanús,35
2011,A,Godoy Cruz,34
2010,A,Vélez Sarsfield,43
2010,A,Lanús,34
2010,A,Godoy Cruz,30

I would like to obtain this:

team:string,points:int
Vélez Sarsfield,82
Lanús,69
Godoy Cruz,64

That is, a list of teams with the sum of their points.

Is there a way to accomplish that?

Thanks!

julo

unread,
Jul 2, 2011, 5:09:06 PM7/2/11
to bandicoot
Hello Michel,

this is how you can do it:

rel Seasons {
year: int,
league: string,
team: string,
points: int,
}

fn AggregatePerTeam(s: Seasons): rel {team: string, points: int}
{
teams := s project(team);
return (s, teams) summary(points = add(points, 0));
}


To calculate the aggregation the "summary" operator is used. It
summarizes all the tuples in the relation "s" per "teams". In other
words, for every team in the "teams" variable it adds up the "points".

An another example could be a calculation of points not only per team
but also per year:

fn AggregatePerTeamYear(s: Seasons): rel {team: string, year: int,
points: int}
{
teams := s project(team, year);
return (s, teams) summary(points = add(points, 0));
}


I hope this is useful.


Regards,
Julius.

Michel Martens

unread,
Jul 2, 2011, 5:46:19 PM7/2/11
to band...@googlegroups.com
On Sat, Jul 2, 2011 at 6:09 PM, julo <ju...@bandilab.org> wrote:
> Hello Michel,
>
> this is how you can do it:
>
> rel Seasons {
>  year: int,
>  league: string,
>  team: string,
>  points: int,
> }
>
> fn AggregatePerTeam(s: Seasons): rel {team: string, points: int}
> {
>    teams := s project(team);
>    return (s, teams) summary(points = add(points, 0));
> }
>
>
> To calculate the aggregation the "summary" operator is used. It
> summarizes all the tuples in the relation "s" per "teams". In other
> words, for every team in the "teams" variable it adds up the "points".
>
> An another example could be a calculation of points not only per team
> but also per year:
>
> fn AggregatePerTeamYear(s: Seasons): rel {team: string, year: int,
> points: int}
> {
>    teams := s project(team, year);
>    return (s, teams) summary(points = add(points, 0));
> }
>
>
> I hope this is useful.

It was very useful, that's plain genius :-)

Thanks a lot, I'm having a lot of fun with this.

Reply all
Reply to author
Forward
0 new messages