Get SUM from a calculation

Hello,

I’m trying to get a total price for a maintenance by summing all amount of work multilplied by work price (grand total)
I was following this topics and video: https://www.youtube.com/watch?v=bZQ4_TTw0kg&t=13s and Summing values in Table but none of them helped me.

  • I have database query which outputs all works
  • I’m trying to set a value based on a collection {{query_get_expenses.sum('price_expense*amount_expense')}}

    In the end I get result 0, but if I’m posting just this code: {{query_get_expenses.sum('price_expense')}} I get sum of all prices

Please help me our because this simple actions took me already 2 days and making me nuts.

Have you tried {{query_get_expenses.sum('price_expense' * 'amount_expense')}}

Hello. Yes, still getting 0 as a result.

Are you attempting this server side or on the front end? If your fields are generic strings then you may need to use the .toNumber() formatter.

I’m trying to get grant total in front-ent side.
.toNumber() just returns nothing.

Here is my data binding:

But 0 returned even on back-end side:

Full code
{

  "meta": {

    "$_GET": [

      {

        "type": "text",

        "name": "sort"

      },

      {

        "type": "text",

        "name": "dir"

      }

    ],

    "$_POST": [

      {

        "type": "text",

        "name": "test"

      }

    ]

  },

  "exec": {

    "steps": [

      {

        "name": "project",

        "module": "core",

        "action": "setvalue",

        "options": {

          "value": 3

        },

        "outputType": "number"

      },

      {

        "name": "query_get_expenses",

        "module": "dbconnector",

        "action": "select",

        "options": {

          "connection": "db",

          "sql": {

            "type": "SELECT",

            "columns": [

              {

                "table": "expenses",

                "column": "amount_expense"

              },

              {

                "table": "expenses",

                "column": "price_expense"

              }

            ],

            "table": {

              "name": "expenses"

            },

            "joins": [],

            "wheres": {

              "condition": "AND",

              "rules": [

                {

                  "id": "expenses.service",

                  "field": "expenses.service",

                  "type": "double",

                  "operator": "equal",

                  "value": "{{project}}",

                  "data": {

                    "table": "expenses",

                    "column": "service",

                    "type": "text"

                  },

                  "operation": "="

                }

              ],

              "conditional": null,

              "valid": true

            },

            "query": "SELECT amount_expense, price_expense\nFROM expenses\nWHERE service = :P1 /* {{project}} */",

            "params": [

              {

                "operator": "equal",

                "type": "expression",

                "name": ":P1",

                "value": "{{project}}"

              }

            ]

          }

        },

        "output": true,

        "meta": [

          {

            "name": "amount_expense",

            "type": "number"

          },

          {

            "name": "price_expense",

            "type": "number"

          }

        ],

        "outputType": "array"

      },

      {

        "name": "test_value_copy",

        "module": "core",

        "action": "setvalue",

        "options": {

          "value": "{{query_get_expenses.sum('price_expense * amount_expense')}}"

        },

        "outputType": "number",

        "output": true

      }

    ]

  }

}

So after private conversation with @max_gb solution was:

  • Use repeat based on db query
  • In repeat set value to calculate total for each array entry
  • After repeat calculate grand total based on repeat

2 Likes