KoolReport's Forum

Official Support Area, Q&As, Discussions, Suggestions and Bug reports.
Forum's Guidelines

Multiple aggregated columns #2742

Open BenSan opened this topic on on Jun 28, 2022 - 1 comments

BenSan commented on Jun 28, 2022

Hello

I am wanting to aggregate several columns in one query My SQL is like this $sql = "SELECT P.Name, C.Position, C.PrizeMoney FROM people AS P LEFT JOIN competitions as C ON P.PeopleId = C.PeopleId

This produces data like this

Name.    Position.    PrizeMoney
John       First           30
John       First           30
John       Second      20
Phil         Second      20 
Pete        Third          10

I want to use the group facility to output data like this - (apologies for formatting!)

Name.    Position.      Positions.     TotalPrizeMoney
John       First                     2                   60
John       Second                1                   20
Phil         Second                1                   20
Pete        Third                    1                   10

So , I want to count the Positions, and sum the prize money by grouping on the first two columns.

My code below counts the positions, but doesn't total the prize money - I just get a 0 in the first TotalPrizeMoney column

$sql = "select P.Name, C.Position, C.PrizeMoney FROM people AS P LEFT JOIN competitions as C ON P.PeopleId = C.PeopleId";
       
        $this->src('people')
        ->query($sql)
        ->pipe(new Group(array(
            "by" => ["Name","Position"]
            ,"count" => "Positions"
            ,"sum" => "TotalPrizeMoney"
        )))
        ->pipe($this->dataStore('results'));

Thank you in advance.

Sebastian Morales commented on Jun 29, 2022

There's no column with name "TotalPrizeMoney" in your original data. Use "sum" => "PrizeMoney" instead. You can change the column name or label after the Group process:

https://www.koolreport.com/docs/processes/columnrename/

Build Your Excellent Data Report

Let KoolReport help you to make great reports. It's free & open-source released under MIT license.

Download KoolReport View demo
None yet

None