我有这个密码
SELECT "School"."name" AS "School",
count(distinct "User"."id") filter (where "public"."user_tokens"."app_name" = 'A') AS "A",
count(distinct "User"."id") filter (where "public"."user_tokens"."app_name" = 'B') AS "B",
count(distinct "User"."id") filter (where "public"."user_tokens"."app_name" = 'C') AS "C",
count(distinct "User"."id") filter (where "public"."user_tokens"."app_name" = 'D') AS "D"
FROM "public"."user_tokens"
LEFT JOIN "public"."users" "User" ON "public"."user_tokens"."user_id" = "User"."id"
LEFT JOIN "public"."user_roles" "User_2" ON "public"."user_tokens"."user_id" = "User_2"."user_id"
LEFT JOIN "public"."roles" "Role" ON "User_2"."role_id" = "Role"."id" LEFT JOIN "public"."schools" "School" ON "User_2"."school_id" = "School"."id"
GROUP BY "School"."name"
ORDER BY "B" desc
结果如下:
School A B C D
--------------------------------------------------
P 5 6 10 6
Q 1 0 0 0
R 2 7 0 6
S 0 8 9 0
是否可以隐藏包含值“0”的整行?在这种情况下,结果应该是学校p只。
之后,如何计算不含零值的“学校”?对于这种情况,结果应该是1
谢谢您。
3条答案
按热度按时间n3ipq98p1#
to94eoyn2#
此外,还可以创建列
A
,B
,C
,和D
可通过将查询作为另一个查询的表表达式括起来进行操作SELECT
. 在这外面SELECT
通过添加筛选 predicate 可以很容易地筛选出行:因此,您的查询可能如下所示:
如果您希望避免查询中的表达式冗余,那么这对于更复杂的表达式非常有用。
wsxa1bj13#
你有用处吗
having
从句并重复表达尽管postgres允许在
GROUP BY
,它不允许在HAVING
子句(在我看来,扩展sql标准的一种奇怪的方式)。现在,您还可以简化和改进查询。首先,你需要火柴,所以用
inner join
. 第二,使用表别名。我还去掉了双引号——这是个非常糟糕的主意: