我是 Postgres 的新手,我在这个查询中遇到了困难:
我有这个查询:
SELECT "employees_employee"."primary_title",
"employees_employee"."primary_role_id",
"employees_organization"."benchmark_organization_id" AS "benchmark_organization",
COUNT("employees_employee"."id") FILTER (WHERE "employees_employee"."role_mapping_id" IS NULL) AS "count",
ROW_NUMBER() OVER (PARTITION BY "employees_organization"."benchmark_organization_id", "employees_employee"."primary_role_id" ORDER BY COUNT("employees_employee"."id") FILTER (WHERE "employees_employee"."role_mapping_id" IS NULL) DESC) AS "rank"
FROM "employees_employee"
LEFT OUTER JOIN "employees_job"
ON ("employees_employee"."primary_job_id" = "employees_job"."id")
LEFT OUTER JOIN "employees_organization"
ON ("employees_job"."organization_id" = "employees_organization"."id")
WHERE ("employees_employee"."blueprint_id" = '58b67b16-6890-40d6-8583-210c81647230'::uuid AND "employees_employee"."deleted" IS null)
GROUP BY "employees_employee"."primary_title",
"employees_organization"."benchmark_organization_id",
"employees_employee"."primary_role_id"
上面的查询给了我这样的数据:
primary_title |primary_role_id |benchmark_organization |count|rank|
----------------------------------------------------------------------|------------------------------------|------------------------------------|-----|----|
manager |d56aae96-cc70-4ef7-8db9-a6aa85f1a93c|be3555cc-aace-463a-9095-f11fe6e0a6fc| 1| 1|
Unknown Title | |be3555cc-aace-463a-9095-f11fe6e0a6fc| 1| 1|
consultant |0b15c7bd-ce3d-4741-b219-77f15a46df09| | 262| 1|
associate consultant |0b15c7bd-ce3d-4741-b219-77f15a46df09| | 230| 2|
senior associate consultant |0b15c7bd-ce3d-4741-b219-77f15a46df09| | 67| 3|
associate consultant intern |0b15c7bd-ce3d-4741-b219-77f15a46df09| | 21| 4|
incoming associate consultant |0b15c7bd-ce3d-4741-b219-77f15a46df09| | 16| 5|
management consultant |0b15c7bd-ce3d-4741-b219-77f15a46df09| | 10| 6|
senior consultant |0b15c7bd-ce3d-4741-b219-77f15a46df09| | 8| 7|
junior consultant |0b15c7bd-ce3d-4741-b219-77f15a46df09| | 2| 8|
specialist consultant |0b15c7bd-ce3d-4741-b219-77f15a46df09| | 2| 9|
incoming associate consultant intern |0b15c7bd-ce3d-4741-b219-77f15a46df09| | 2| 10|
consulting |0b15c7bd-ce3d-4741-b219-77f15a46df09| | 2| 11|
recruitment consultant |0b15c7bd-ce3d-4741-b219-77f15a46df09| | 1| 12|
director of global consultant talent management |0b15c7bd-ce3d-4741-b219-77f15a46df09| | 1| 13|
industry consultant |0b15c7bd-ce3d-4741-b219-77f15a46df09| | 1| 14|
consultant (private equity group) |0b15c7bd-ce3d-4741-b219-77f15a46df09| | 1| 15|
alliance partner - bulgaria | management consulting |0b15c7bd-ce3d-4741-b219-77f15a46df09| | 1| 16|
associate consultant intern (aci) |0b15c7bd-ce3d-4741-b219-77f15a46df09| | 1| 17|
lenox conyngham scholar at cambridge and incoming associate consultant|0b15c7bd-ce3d-4741-b219-77f15a46df09| | 1| 18|
practice area consultant in peg automation at bain consulting |0b15c7bd-ce3d-4741-b219-77f15a46df09| | 1| 19|
practice area consultant |0b15c7bd-ce3d-4741-b219-77f15a46df09| | 1| 20|
consultor |0b15c7bd-ce3d-4741-b219-77f15a46df09| | 1| 21|
head of talent and consulting ops, nyc |0b15c7bd-ce3d-4741-b219-77f15a46df09| | 1| 22|
case team leader |0b15c7bd-ce3d-4741-b219-77f15a46df09| | 1| 23|
freelance associate consultant |0b15c7bd-ce3d-4741-b219-77f15a46df09| | 1| 24|
associate consultant ii |0b15c7bd-ce3d-4741-b219-77f15a46df09| | 1| 25|
基本上我只是想用一个primary_title为每个primary_role
&benchmark_organization
组合提取员工数量。扭曲的是,我只需要每个类别的前 10 个角色 job_titles 计数(primary_role
&benchmark_organization
组合)
为此,我的方法是为每个类别数据添加一个排名,并在排名列上过滤最终数据。
请注意,列count
&rank
是别名列,值是在运行时计算的。
我只想过滤掉排名小于或等于 10 的记录。
这就像将其添加到 Mysql 或其他一些 RDBMS 中的 where 子句一样简单,但看起来 postgres 不支持 where 子句中的别名列。
有没有其他方法来过滤结果?