gpt4 book ai didi

sql - 计算 CASE WHEN 的输出

转载 作者:行者123 更新时间:2023-11-29 14:27:30 29 4
gpt4 key购买 nike

我需要对 psql 实例的查询输出进行计数。

我已经尝试将它变成许多不同配置的子查询。

我想获取此查询的输出并对新创建的测试组列进行计数。

SELECT userid,
CASE WHEN COUNT(type) filter (WHERE type = 'web') <= COUNT(type) filter (WHERE type = 'mobile') THEN 'GROUPA'
ELSE 'GROUPB'
END AS testgroup FROM logins
GROUP BY userid
ORDER BY userid;

输出

enter image description here

期望的输出

enter image description here

最佳答案

一种方法是子查询:

SELECT testgroup, COUNT(*)
FROM (SELECT userid,
(CASE WHEN COUNT(type) filter (WHERE type = 'web') <= COUNT(type) filter (WHERE type = 'mobile')
THEN 'GROUPA'
ELSE 'GROUPB'
END) AS testgroup
FROM logins
GROUP BY userid
) u
GROUP BY testgroup

关于sql - 计算 CASE WHEN 的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56281423/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com