这是来自我的数据库的示例信息,因此可以显示我需要完成的全部图片
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的总计数,然后除以该storelocation的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
答案 0 :(得分:2)
将总计数查询设为Subquery
并将其除以storelocation
组计数
SELECT storelocation,
(SELECT CONVERT(DECIMAL(18, 4), Count(1))
FROM #Information) / Count(1)
FROM #Information
GROUP BY storelocation
答案 1 :(得分:1)
试试这个:
DECLARE @TotalCount as int;
SELECT @TotalCount = COUNT(salesID)
FROM #information;
SELECT storeLocation, (@TotalCount / COUNT(salesID)) AS division
FROM #information
GROUP BY storeLocation;
答案 2 :(得分:1)
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')
DECLARE @TotalCount AS INT
SELECT @TotalCount = CONVERT(DECIMAL(18, 4), COUNT(salesID))
FROM #information
SELECT storelocation,
@TotalCount / CONVERT(DECIMAL(18, 4), COUNT(storelocation)) AS Division
FROM #Information
GROUP BY
storelocation