I'm using json_agg in Postgres like this
json_agg((e."name",e."someOtherColum",e."createdAt") order by e."createdAt" DESC )
But I want to limit how many rows will be aggregated into JSON. I want to write something like this
json_agg((e."name",e."someOtherColum",e."createdAt") order by e."createdAt" DESC LIMIT 3)
Is it possible in some way?
This is full query
SELECT e."departmentId",
json_agg((e."name",e."someOtherColum",e."createdAt") order by e."createdAt" DESC ) as "employeeJSON"
FROM "Employee" e
GROUP BY e."departmentId"
So I want to achieve department with first three employees for each department.