Sum of column based on another Column

Sum of column based on another Column

SradesignSradesign Posts: 25Questions: 4Answers: 1
edited September 18 in Free community support

I want to see sum of a column based on another column value:

var ftdA = rows
                    .data()
                    .pluck(7)
                    .reduce( function (a, b) {
                        if (a == "FTD" || b == "FTD") {
                            var ftda2 = rows
                                .data()
                                .pluck(6)
                                .reduce( function (a, b) {
                                    var ftda3 = a + b*1; 
                                    return ftda3;
                                }, 0);
                            ftda = ftda2.toFixed(2);
                        }
                        return ftda;
                    }, 0);

I used something like this but is not working still sum all the values on that column.

Mind you I use RowGroup

Edited by Kevin:  Syntax highlighting. Details on how to highlight code using markdown can be found in this guide

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 10,565Questions: 25Answers: 2,407

    In order to understand the data you have and what you want to accomplish please build a simple test case representing your data and the RowGroup. This way we can work with your data to help you achieve the desired result.
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

  • SradesignSradesign Posts: 25Questions: 4Answers: 1
    edited September 18

    This is the live test as you can see the number of System Architect's are showing up but I want their sum of age
    http://live.datatables.net/zezuxali/2/

  • kthorngrenkthorngren Posts: 10,565Questions: 25Answers: 2,407

    Thanks for the test case. I can see that ftdN results in 0 and ftdA results in a value. Please provide details of where the problem is and what you are expecting. The more details you provide the easier it will be for us to help.

    Kevin

  • SradesignSradesign Posts: 25Questions: 4Answers: 1
    edited September 18

    I changed the test case please look at it again it should only show the total age of the system architects

  • kthorngrenkthorngren Posts: 10,565Questions: 25Answers: 2,407
    Answer ✓

    Looks like we cross posted :smiley:

    I don't think reduce() is going to work for what you want. I think rows().every() is a good choice. Updated your example here:
    http://live.datatables.net/zezuxali/4/edit

    Loops through all the rows in the RowGroup and increments the ftdA variable by the age of each System Architect.

    Kevin

  • SradesignSradesign Posts: 25Questions: 4Answers: 1

    Wooow nice Kevin that was an awesome approach

Sign In or Register to comment.