我有一个 MSSQL 表存储,表中包含以下列:
Storeid, NumEmployees
1 125
2 154
3 10
4 698
5 54
6 98
7 87
8 100
9 58
10 897
有人可以帮助我使用 SQL 查询来生成占员工总数(NumEmployees)30% 的顶级商店(storeID)吗?
我有一个 MSSQL 表存储,表中包含以下列:
Storeid, NumEmployees
1 125
2 154
3 10
4 698
5 54
6 98
7 87
8 100
9 58
10 897
有人可以帮助我使用 SQL 查询来生成占员工总数(NumEmployees)30% 的顶级商店(storeID)吗?
WITH cte
AS (SELECT storeid,
numemployees,
( numemployees * 100 ) / SUM(numemployees) OVER (PARTITION BY 1)
AS
percentofstores
FROM stores)
SELECT *
FROM cte
WHERE percentofstores >= 30
ORDER BY numemployees desc
不使用 SUM/OVER 的替代方案
SELECT s.storeid, s.numemployees
FROM (SELECT SUM(numemployees) AS [tots]
FROM stores) AS t,
stores s
WHERE CAST(numemployees AS DECIMAL(15, 5)) / tots >= .3
ORDER BY s.numemployees desc
请注意,在第二个版本中,我决定在除法之前不乘以 100。这需要强制转换为十进制,否则它将被隐式转换为 int 导致不返回任何记录
此外,我并不完全清楚您是否想要这个,但您可以添加TOP 1
到这两个查询中,它将结果限制为具有最大商店数量超过 30% 的结果
更新
根据您的评论,听起来像是在解释凯文
你想要这些行,从员工最多的商店开始,一直到至少有 30 %
这很困难,因为它需要一个运行百分比并且它是一个装箱问题,但这确实有效。注意我已经包含了另外两个测试用例(百分比完全等于并且它刚刚超过前两个的组合)
DECLARE @percent DECIMAL (20, 16)
SET @percent = 0.3
--Other test values
--SET @percent = 0.6992547128452433
--SET @percent = 0.6992547128452434
;WITH sums
AS (SELECT DISTINCT s.storeid,
s.numemployees,
s.numemployees + Coalesce(SUM(s2.numemployees) OVER (
PARTITION
BY
s.numemployees), 0)
runningsum
FROM stores s
LEFT JOIN stores s2
ON s.numemployees < s2.numemployees),
percents
AS (SELECT storeid,
numemployees,
runningsum,
CAST(runningsum AS DECIMAL(15, 5)) / tots.total
running_percent,
Row_number() OVER (ORDER BY runningsum, storeid ) rn
FROM sums,
(SELECT SUM(numemployees) total
FROM stores) AS tots)
SELECT p.storeID,
p.numemployees,
p.running_percent,
p.running_percent,
p.rn
FROM percents p
CROSS JOIN (SELECT MAX(rn) rn
FROM percents
WHERE running_percent = @percent) exactpercent
LEFT JOIN (SELECT MAX(rn) rn
FROM percents
WHERE running_percent <= @percent) underpercent
ON p.rn <= underpercent.rn
OR ( exactpercent.rn IS NULL
AND p.rn <= underpercent.rn + 1 )
WHERE
underpercent.rn is not null or p.rn = 1