I have this array of JSON objects :
let merged = [
{
user_id: 2,
year: 2021,
month: 'July',
gross_subscription_amount: 650,
gross_redemption_amount: 0,
gender: 'M',
state: '01',
age: '18',
number_of_investors: 0
},
{
user_id: 1,
year: 2026,
month: 'October',
gross_subscription_amount: 0,
gross_redemption_amount: 10,
gender: 'M',
state: '01',
age: '20',
number_of_investors: 0
},
{
user_id: 2,
year: 2021,
month: 'July',
gross_subscription_amount: 79,
gross_redemption_amount: 0,
gender: 'M',
state: '01',
age: '18',
number_of_investors: 0
},
{
user_id: 3,
year: 2021,
month: 'July',
gross_subscription_amount: 79,
gross_redemption_amount: 0,
gender: 'M',
state: '01',
age: '18',
number_of_investors: 0
},
{
user_id: 2,
year: 2021,
month: 'July',
gross_subscription_amount: 79,
gross_redemption_amount: 0,
gender: 'M',
state: '01',
age: '18',
number_of_investors: 0
}
]
I would like to do an equivalent of this SQL Query:
SELECT year, month, gender, state, age,
SUM(gross_subscription_amount),
SUM(gross_redemption_amount),
COUNT(DISTINCT user_id)
GROUP BY year, month, gender, state, age
where each user (seen from their 'user_id'
) are grouped based on a combination of these categories:
year, month, gender, stage, age (in this order)
and get a sum of their 'gross_subscription_amount'
and 'gross_redemption_amount'
.
My issue here is I am unable to execute the COUNT(DISTINCT) part.
The code I tried is below:
let merged = [
{
user_id: 2,
year: 2021,
month: 'July',
gross_subscription_amount: 650,
gross_redemption_amount: 0,
gender: 'M',
state: '01',
age: '18',
number_of_investors: 0
},
{
user_id: 1,
year: 2026,
month: 'October',
gross_subscription_amount: 0,
gross_redemption_amount: 10,
gender: 'M',
state: '01',
age: '20',
number_of_investors: 0
},
{
user_id: 2,
year: 2021,
month: 'July',
gross_subscription_amount: 79,
gross_redemption_amount: 0,
gender: 'M',
state: '01',
age: '18',
number_of_investors: 0
},
{
user_id: 3,
year: 2021,
month: 'July',
gross_subscription_amount: 79,
gross_redemption_amount: 0,
gender: 'M',
state: '01',
age: '18',
number_of_investors: 0
},
{
user_id: 2,
year: 2021,
month: 'July',
gross_subscription_amount: 79,
gross_redemption_amount: 0,
gender: 'M',
state: '01',
age: '18',
number_of_investors: 0
}
]
var helper = {};
var result = merged.reduce(function(r, o) {
var key = o.year + '-' + o.month + '-' + o.gender + '-' + o.age + '-'+ o.state;
if(!helper[key]) {
helper[key] = Object.assign({}, o); // create a copy of o
r.push(helper[key]);
} else {
helper[key].gross_subscription_amount += o.gross_subscription_amount;
helper[key].gross_redemption_amount += o.gross_redemption_amount;
helper[key].number_of_investors++;
}
return r;
}, []);
console.log(result);
From the result, the first group:
Year - 2021 Month- July Gender - M State - 01 Age - 18
There are supposed to be 2 unique users in this group (user_id: 2 & 3) but it is returning the wrong number of investors
value.
Expected Result is below:
[
{
"year": 2021,
"month": "July",
"gross_subscription_amount": 887,
"gross_redemption_amount": 0,
"gender": "M",
"state": "01",
"age": "18",
"number_of_investors": 2
},
{
"year": 2026,
"month": "October",
"gross_subscription_amount": 0,
"gross_redemption_amount": 10,
"gender": "M",
"state": "01",
"age": "20",
"number_of_investors": 0
}
]
Thanks in advance.