Agreggation Sum in Prolog

38 views
Skip to first unread message

Diogo Oliv

unread,
Sep 27, 2015, 2:24:09 PM9/27/15
to SWI-Prolog
I have a database in Prolog like this (kind of like a table in excel):

invoice(number, locality, value).


invoice
(1, madrid, 100).
invoice
(2, lisbon, 200).
invoice
(3, london, 300).
invoice
(4, madrid, 300).
invoice
(5, lisbon, 200).
invoice
(6, paris, 100).


How can I get the agreggate sum for locality?

The answer should be something like doing a pivot table in Excel..

Lisbon total:400 Madrid total: 400 Paris total: 100 London total: 300

Julio Di Egidio

unread,
Sep 27, 2015, 2:53:20 PM9/27/15
to SWI-Prolog
Here is a way with library(aggregate):

?- aggregate(sum(Val), Num^invoice(Num, Loc, Val), Sum).
Loc = lisbon,
Sum = 400 ;
Loc = london,
Sum = 300 ;
Loc = madrid,
Sum = 400 ;
Loc = paris,
Sum = 100.

See the documentation for details...

HTH,

Julio

Reply all
Reply to author
Forward
0 new messages