计数和数学表达时的嵌套情况
如何在下面达到此结果?
ID | id_status | 速率 |
---|---|---|
25 | x | 62.5% |
15 | y | 37.5% |
尝试了
SELECT
COUNT(tab.id) AS id,
tab.status AS id_status,
(CASE
WHEN tab.status = 'X' THEN (25/40) * 100 -- this is where I'm stucked (40 = total of ids)
WHEN tab.status = 'Y' THEN 100 - ((25/40) * 100)
END AS rate
FROM table AS tab
WHERE tab.status in ('X', 'Y')
GROUP BY ROLLUP (tab.status)
how can I achieve this result below?
id | id_status | rate |
---|---|---|
25 | X | 62.5% |
15 | Y | 37.5% |
having tried this
SELECT
COUNT(tab.id) AS id,
tab.status AS id_status,
(CASE
WHEN tab.status = 'X' THEN (25/40) * 100 -- this is where I'm stucked (40 = total of ids)
WHEN tab.status = 'Y' THEN 100 - ((25/40) * 100)
END AS rate
FROM table AS tab
WHERE tab.status in ('X', 'Y')
GROUP BY ROLLUP (tab.status)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用窗口函数获取总数
注释明确的窗口规范,因为默认值通常为
..和当前行
和200,因为lollup
将添加总行。You can use a window function to get total count
Note explicit window specification because the default is generally
.. and current row
and 200 becauserollup
will add the total row.db<>fiddle