This is my table:
project_id | task_id | foo | bar | job_id |
-----------+---------+-----+-----+--------+
1 12 x y 13
1 12 x z 14
1 12 a b 15
1 1210 x y 1211
1 1210 z v 1212
1 1220 aa bb 1221
I trying to write a query where group by by task_id and count job_id per task_id
So I expect something like that:
project_id | task_id | foo | bar | job_id | countJobsperTask |
-----------+---------+-----+-----+--------+------------------+
1 12 x y 13 3
1 12 x z 14 3
1 12 a b 15 3
2 1210 x y 1211 2
2 1210 z v 1212 2
3 1220 aa bb 1221 1
My not very successful query :
select
project_id, task_id, foo, bar, job_id,
count(job_id) as "countJobsperTask"
from
project
group by
project_id, task_id, job_id
GROUP BYterms. If you want to select the original rows (ungrouped), but calculate an aggregateOVERsome similar rows, you can use window functions, as shown by @Pooya's answer. There are also several ways to useGROUP BYin a separate expression and then refer to that in your select list. There is a question about how you want to count job_id (distinct or not). That would need to be specified.