gpt4 book ai didi

sql - 查询中的除法

转载 作者:行者123 更新时间:2023-12-02 04:25:08 24 4
gpt4 key购买 nike

这是来 self 的数据库的示例信息,因此可以显示我需要完成的任务的完整图片

Create Table #Information
(
salesID int,
storelocation varchar(100),
salespersonName varchar(100)
)

Insert Into #Information Values
(1, 'New York', 'Michael'),
(2, 'New York', 'Michael'),
(3, 'New York', 'Michael'),
(4, 'New York', 'Michael'),
(5, 'Texas', 'Richard'),
(6, 'Texas', 'Richard'),
(7, 'Texas', 'Richard'),
(8, 'Texas', 'Richard'),
(9, 'Texas', 'Richard'),
(10, 'Texas', 'Richard'),
(11, 'Washington', 'Sam'),
(12, 'Washington', 'Sam'),
(13, 'Washington', 'Sam'),
(14, 'Washington', 'Sam'),
(15, 'Washington', 'Sam')


SELECT storelocation,
COUNT(salesID/storelocation)
FROM #Information

我想要获取 salesID 的总数,然后除以该商店位置的 salesID。所以我想要发生的划分是

New York - 15/4 = .266
Texas - 15/6 = .4
Washington - 15/5 = .333

我这样做的方式就像这样 - 但这并没有返回准确的结果。

declare @TotalCount as int
select @TotalCount = convert(decimal(18,4), count(salesID))
from #information
Select
convert(decimal(18,4), Count(salesID))/@TotalCount
From #information

最佳答案

将总计数查询设置为子查询,并将其除以storelocation组计数

SELECT storelocation,
(SELECT CONVERT(DECIMAL(18, 4), Count(1))
FROM #Information) / Count(1)
FROM #Information
GROUP BY storelocation

关于sql - 查询中的除法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27644345/

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