My query returns this result, I want to fetch all the records inside rawformdata table with same stateId and append it to object as “formData” property
[{
state: 'Uttar Pradesh',
stateId: 34,
CityCount: 1n,
TotalDataCount: 942n
},
{
state: 'West Bengal',
stateId: 36,
CityCount: 1n,
TotalDataCount: 4204n
}
]
Want it to be like this –
[{
state: 'Uttar Pradesh',
stateId: 34,
CityCount: 1n,
TotalDataCount: 942n,
formData: [all the records with same stateid]
},
{
state: 'West Bengal',
stateId: 36,
CityCount: 1n,
TotalDataCount: 4204n,
formData: [all the records with same stateid]
}
]
Here is my query, it seems like JSON_ARRAYAGG and JSON_OBJECT is not supported in prisma –
const groupedData = await prisma.$queryRaw`
SELECT
r.state,
r.stateId,
COUNT(DISTINCT r.cityId) AS CityCount,
COUNT(r.id) as TotalDataCount,
JSON_ARRAYAGG(
JSON_OBJECT(
'id', r.id,
'name', r.name,
'email', r.email,
)
) AS formData
FROM rawformdata r
GROUP BY r.state, r.stateId;
`;