如何对列进行分组并对列的多个单独值求和?
How to group on column and sum up multiple individual value of column?
下面是MongoDBcollection/table结构:
id - 唯一 ID
阶段 - {id:unique id,name:TRIGGER OTP}
客户 - customer_mobile_no
日期 - 日期
stage 列还包含以下条目
{"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"}
如果我 运行 下面的查询:
[
{"$match":
{"$and":[
{"stage.name":"CREATE REPAYMENT LINK"}]
}
},
{"$group":{_id:"$customer",count:{$sum:1}}},
{"$project":{id:0,Customer:"$_id","CREATE REPAYMENT LINK":"$count"}}
]
我会得到如下输出:
Customer CREATE REPAYMENT LINK
1122334455 2
1123334455 1
1124334455 1
1125334455 3
现在有什么方法可以计算多个值或实现以下输出:
Customer CREATE REPAYMENT LINK TRIGGER OTP OTP FORM
1122334455 2 3 2
1123334455 1 4 1
1124334455 1 1 3
1125334455 3 0 1
最后,我能够回答但不是所有客户。只有一个,通过以下查询:
[
{"$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 ]}
}}
]
如我所料的输出:
Customer OTP FORM TRIGGER OTP VERIFY | RESEND VERIFY OTP LOAN DETAILS
1122334455 12 12 9 4 4
下面是MongoDBcollection/table结构:
id - 唯一 ID
阶段 - {id:unique id,name:TRIGGER OTP}
客户 - customer_mobile_no
日期 - 日期
stage 列还包含以下条目
{"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"}
如果我 运行 下面的查询:
[
{"$match":
{"$and":[
{"stage.name":"CREATE REPAYMENT LINK"}]
}
},
{"$group":{_id:"$customer",count:{$sum:1}}},
{"$project":{id:0,Customer:"$_id","CREATE REPAYMENT LINK":"$count"}}
]
我会得到如下输出:
Customer CREATE REPAYMENT LINK
1122334455 2
1123334455 1
1124334455 1
1125334455 3
现在有什么方法可以计算多个值或实现以下输出:
Customer CREATE REPAYMENT LINK TRIGGER OTP OTP FORM
1122334455 2 3 2
1123334455 1 4 1
1124334455 1 1 3
1125334455 3 0 1
最后,我能够回答但不是所有客户。只有一个,通过以下查询:
[
{"$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 ]}
}}
]
如我所料的输出:
Customer OTP FORM TRIGGER OTP VERIFY | RESEND VERIFY OTP LOAN DETAILS
1122334455 12 12 9 4 4