'how do I set up a grouping query on the server side. I want set up the query so I can use it for a pie chart in front end, using d3js
I am using this query to count and group occupation types. I need to do this in order to create a pie chart for the client side for the front end. I don't think I need to input anything, I just want to get the information sorted and counted. User_Id is just referenced just in case I get any errors with authorization of the jwt token. The user_id is not referenced in the table that I am pulling information from, it's just there for authorization purposes.
router.get("/occupation/:user_id", authorization, async (req, res) => {
try {
console.log(req);
const result = await pool.query(
"SELECT occupation,COUNT(occupation) FROM resources GROUP BY occupation;",
[req.params.user_id][req.body.occupation]
// [req.body.json
// ]
);
console.log(req.body);
res.status(200).json({
status: "success",
data: {
occupation: result.rows, //this gets the one row we need
},
});
} catch (err) {
console.error(err.message);
}
});
Solution 1:[1]
router.get("/occ", authorization, async (req, res) => {
try {
console.log(req);
const result = await pool.query(
// "SELECT occupation,COUNT(occupation) FROM resources GROUP BY occupation;",
"SELECT occupation,COUNT(occupation) as values FROM resources GROUP BY occupation"
);
console.log(req);
console.log(result);
res.status(200).json({
status: "success",
data: {
occupationResults: result.rows, //this gets the one row we need
},
});
} catch (err) {
console.error(err.message);
}
});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | alexey aulov |
