Search code examples
mongodbnosqlmetabase

How to group on column and sum up multiple individual value of column?


So below is MongoDB collection/table structure:

id - unique id
stage - {id:unique id,name:TRIGGER OTP}
customer - customer_mobile_no
date - date

stage column also contains below entries

{"id":"abc","name":"CREATE REPAYMENT LINK"}
{"id":"abl","stage.name":"OTP FORM"}
{"id":"atc","stage.name":"TRIGGER OTP"},
{"id":"akc","stage.name":"VERIFY | RESEND"}
{"id":"agc","stage.name":"VERIFY OTP"},
{"id":"afc","stage.name":"LOAN DETAILS"},
{"id":"aac","stage.name":"PAYMENT SCREEN"}

if i run below query:

[
    {"$match":
            {"$and":[
                {"stage.name":"CREATE REPAYMENT LINK"}]
            }
    },
    {"$group":{_id:"$customer",count:{$sum:1}}},
    {"$project":{id:0,Customer:"$_id","CREATE REPAYMENT LINK":"$count"}}
]

i will get output like:

Customer    CREATE REPAYMENT LINK
1122334455      2
1123334455      1
1124334455      1
1125334455      3

now is there any way to count multiple values or achieve the below output:

Customer    CREATE REPAYMENT LINK    TRIGGER OTP    OTP FORM
1122334455      2                       3              2
1123334455      1                       4              1
1124334455      1                       1              3
1125334455      3                       0              1

Solution

  • finally, I'm able to get to answer but not for all customers. only one, by below query:

    [
        {"$match":
            {"$and":[
                {"$or":[
                    {"stage.name":"CREATE REPAYMENT LINK"},
                    {"stage.name":"OTP FORM"},
                    {"stage.name":"TRIGGER OTP"},
                    {"stage.name":"VERIFY | RESEND"}
                    {"stage.name":"VERIFY OTP"},
                    {"stage.name":"LOAN DETAILS"},
                    {"stage.name":"PAYMENT SCREEN"},
                    {"stage.name":"FINAL SCREEN"},
                    {"stage.name":"process timeout"}]
                },{"$and":[
                    {"customer":{"$eq":{{mobile_no}}}}]
                }]
            }    
        },
        {"$facet":{
            "CREATE_REPAYMENT_LINK": [
                {"$match":{"$and":[{"stage.name":"CREATE REPAYMENT LINK"}]}},
                {"$group": {_id: "$customer",count:{$sum:1}}}],
            
            "OTP_FORM": [
                {"$match":{"$and":[{"stage.name":"OTP FORM"}]}},
                {"$group": {_id: "$customer",count:{$sum:1}}}],
            
            "TRIGGER_OTP": [
                {"$match":{"$and":[{"stage.name":"TRIGGER OTP"}]}},
                {"$group": {_id: "$customer",count:{$sum:1}}}],
                
            "VERIFY_RESEND": [
                {"$match":{"$and":[{"stage.name":"VERIFY | RESEND"}]}},
                {"$group": {_id: "$customer",count:{$sum:1}}}],    
                
            "VERIFY_OTP": [
                {"$match":{"$and":[{"stage.name":"VERIFY OTP"}]}},
                {"$group": {_id: "$customer",count:{$sum:1}}}],
                
            "LOAN_DETAILS": [
                {"$match":{"$and":[{"stage.name":"LOAN DETAILS"}]}},
                {"$group": {_id: "$customer",count:{$sum:1}}}],
                
            "PAYMENT_SCREEN": [
                {"$match":{"$and":[{"stage.name":"PAYMENT SCREEN"}]}},
                {"$group": {_id: "$customer",count:{$sum:1}}}],
                
            "FINAL_SCREEN": [
                {"$match":{"$and":[{"stage.name":"FINAL SCREEN"}]}},
                {"$group": {_id: "$customer",count:{$sum:1}}}]
            
            }
        },
        {"$project":{_id:0,"Customer":{{mobile_no}},
                    "CREATE REPAYMENT LINK":{$arrayElemAt: [ "$CREATE_REPAYMENT_LINK.count", 0 ]},
                    "OTP FORM":{$arrayElemAt: [ "$OTP_FORM.count", 0 ]},
                    "TRIGGER OTP":{$arrayElemAt: [ "$TRIGGER_OTP.count", 0 ]},
                    "VERIFY | RESEND":{$arrayElemAt: [ "$VERIFY_RESEND.count", 0 ]},
                    "VERIFY OTP":{$arrayElemAt: [ "$VERIFY_OTP.count", 0 ]},
                    "LOAN DETAILS":{$arrayElemAt: [ "$LOAN_DETAILS.count", 0 ]},
                    "PAYMENT SCREEN":{$arrayElemAt: [ "$PAYMENT_SCREEN.count", 0 ]},
                    "FINAL SCREEN":{$arrayElemAt: [ "$FINAL_SCREEN.count", 0 ]}
        }}
    ]
    

    Output as i expected:

    Customer    OTP FORM    TRIGGER OTP VERIFY | RESEND     VERIFY OTP  LOAN DETAILS
    1122334455     12           12          9               4            4