tables::ftable::agg

template <is_table T,is_tuple_unique Gs>
template<typename... Ss,typename... Fs>
requires (
  (sizeof...(Ss) == sizeof...(Fs))
  && ( all_in<std::tuple<Ss...>,tuple_ext::remove_t<Gs,typename T::ts_t>> )
  && ( is_col_to_prim_f<Fs,typename T::p_of_t<Ss>> && ... )
)
auto
ftable<T,Gs>::agg(const Fs... fs) const

Applies aggregate function to groups.

Example

Code

#include <cpptables/table.hh>
#include <iostream>

using namespace tables;
using namespace std;

struct col0  { constexpr static char const * const name = "col0"; };
struct col1  { constexpr static char const * const name = "col1"; };
struct col2  { constexpr static char const * const name = "col2"; };

using table_t = table<
  pair<col0,string_view>,
  pair<col1,unsigned>,
  pair<col2,double>
>;

void fby_agg()
{
  const table_t t(
    {"A", "B", "C", "A", "B", "C", "A", "B", "C"},
    {1,10,100,2,20,200,3,30,300},
    {100.0,200.0,300.0,400.0,500.0,600.0,700.0,800.0,900.0}
  );

  const auto fsum = [](const column<auto>& xs) { return xs.sum(); };
  const auto t1 = t.fby<col0>().agg<col1>(fsum);
  cout << t1 << "\n";

  const auto favg = [](const column<auto>& xs) { return xs.avg(); };
  const auto t2 = t.fby<col0>().agg<col1,col2>(fsum,favg);
  cout << t2 << "\n";
}

Output

col0 col1
---------
   A    6
   B   60
   C  600

col0 col1 col2
--------------
   A    6  400
   B   60  500
   C  600  600