Skip to content

Instantly share code, notes, and snippets.

@fabiosussetto
Created September 8, 2017 11:31
Show Gist options
  • Save fabiosussetto/b767727e5fb7b2badccd5987f422138a to your computer and use it in GitHub Desktop.
Save fabiosussetto/b767727e5fb7b2badccd5987f422138a to your computer and use it in GitHub Desktop.
{
"tot": 1,
"date": "2017-09-08",
"category_id": 3,
"name": "Uniform Issues"
},
{
"tot": 1,
"date": "2017-09-06",
"category_id": 3,
"name": "Uniform Issues"
},
{
"tot": 2,
"date": "2017-09-08",
"category_id": 2,
"name": "HR Issues"
}
SELECT g.category_name, json_agg(g) as dates FROM
(
SELECT count(*) AS tot, i.created_at::date AS date, ic.id AS category_id, ic.name AS category_name
FROM {Issue} i
INNER JOIN {IssueCategory} ic ON ic.id = i.issue_category_id
GROUP BY i.issue_category_id, ic.name, ic.id, i.created_at::date
) g
GROUP BY g.category_name
{
"category_name": "Uniform Issues",
"dates": [
{
"tot": 1,
"date": "2017-09-08",
"category_id": 3,
"category_name": "Uniform Issues"
},
{
"tot": 1,
"date": "2017-09-06",
"category_id": 3,
"category_name": "Uniform Issues"
}
]
},
{
"category_name": "HR Issues",
"dates": [
{
"tot": 2,
"date": "2017-09-08",
"category_id": 2,
"category_name": "HR Issues"
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment