Getting sum of values grouped by predicate

Actually gave only one suggestion.

This is total of items? if so, I think can’t be done by this query. You have to do two blocks.

Mutation

{
   "set":[
      {
         "isPaymentType":true,
         "Payment.Type": "Cash",
         "uid":"_:cash"
      },
      {
         "isPaymentType":true,
        "Payment.Type": "Card",
         "uid":"_:card"
      },
      {
         "isBill":true,
         "items":[
            {
               "amount":10
            },
            {
               "amount":2
            }
         ],
         "payments":[
            {
               "amount":20,
               "paymentType":{
                  "uid":"_:cash"
               }
            }
         ]
      },
      {
         "isBill":true,
         "items":[
            {
               "amount":5
            },
            {
               "amount":3
            }
         ],
         "payments":[
            {
               "amount":5,
               "paymentType":{
                  "uid":"_:card"
               }
            },
            {
               "amount":3,
               "paymentType":{
                  "uid":"_:cash"
               }
            }
         ]
      }
   ]
}

Query

{
  var(func:has(payments)) {
   payments @groupby(paymentType){ 
    A as count(uid)
  }
}
  
   q(func: uid(A)) @normalize
  {
    Payment.Type : Payment.Type
    isPaymentType : isPaymentType
    ~paymentType {
  		AM as amount
 		 }
    Total : sum(val(AM))
  }
}

Result

{
  "data": {
    "q": [
      {
        "Payment.Type": "Cash",
        "isPaymentType": true,
        "Total": 23
      },
      {
        "Payment.Type": "Card",
        "isPaymentType": true,
        "Total": 5
      }
    ]
  }
}
1 Like