gpt4 book ai didi

MySQL计算百分比,我可以使用变量吗?

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

我有一个查询,该查询返回 session 数量以及这些 session 中下的订单数量。很简单,但我想做的是显示下订单的 session 百分比。

SELECT COUNT(*) FROM table.session //Save this as variable 'Amount1'?
WHERE ses_Date BETWEEN ('2006/01/01') AND ('2011/01/01')
UNION ALL
SELECT COUNT(*) FROM table.session
WHERE Ses_ID
IN
(
SELECT Ses_ID FROM session //Save this as variable 'Amount2'?
JOIN table.order ON order.Session_ID = session.Ses_ID
WHERE ses_Date BETWEEN ('2006/01/01') AND ('2011/01/01')
GROUP by ses_ID
)

//Then do this, Display the percentage of the two
union all
Select(SUM(amount2) /SUM(amount1) * 100)

在代码的评论中,我写了理论上我想做的事情,希望有所帮助。

最佳答案

你可以这样做:

SELECT COUNT(*) / (SELECT COUNT(*) 
FROM table.session
WHERE ses_Date BETWEEN ('2006/01/01') AND ('2011/01/01')) * 100

FROM table.session
WHERE Ses_ID
IN
(
SELECT Ses_ID FROM session //Save this as variable 'Amount2'?
JOIN table.order ON order.Session_ID = session.Ses_ID
WHERE ses_Date BETWEEN ('2006/01/01') AND ('2011/01/01')
GROUP by ses_ID
)

关于MySQL计算百分比,我可以使用变量吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13401231/

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