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
Working Demo
替代。这需要铸造小数否则它会隐式转换为int导致没有记录返回
另外我不完全清楚,你想要这个,但你可以添加TOP 1
两个查询,它会限制结果只是在一个设有专卖店的最大#有超过30%
UPDATE
根据您的意见听起来套用Kevin
您想要的行,开始在商店最员工和工作,直到你至少有30%
这是困难的,因为它需要一个正在运行的百分比但其一个装箱问题这样做的工作。注意我已经包括另外两个测试案例(其中百分比恰好等于和它只是在上面两个组合)
Working Demo
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
我曾尝试选择SUM(numemploees)..发现有30%,然后选择从那个数字开始。 – user1204195 2012-04-06 17:46:29
我对你想要的有点困惑。你是否需要这些行,从storeid 1开始,直到总共达到30%?哪个会给你店1,2,3,4。或者你是否想要这些行,从拥有最多员工的商店开始,直到你有30%的工作?哪个会让你的商店10?或者所有有30%以上的商店?这会给你4和10. – Kevin 2012-04-06 18:36:45