<div class="notebook"> <div class="nb-cell program" data-background="true" name="p1"> p( 301, 1, 2 ). p( 201, 5, 2 ). p( 501, 1, 5 ). p( 401, 1, 4 ). patients(L) :- findall(p(A,B,C), p(A,B,C), L). </div> <div class="nb-cell program" name="p2"> group_by_arg3(Terms, Grouped) :- map_list_to_pairs(arg(3), Terms, Keyed), keysort(Keyed, Sorted), group_pairs_by_key(Sorted, KeyGrouped), pairs_values(KeyGrouped, Grouped). </div> <div class="nb-cell query" name="q1"> projection([G]), patients(L), group_by_arg3(L, G). </div> </div>