SQL查询选择总数的百分比

时间:2012-04-06 17:38:09

标签: sql sql-server

我有一个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)吗?

1 个答案:

答案 0 :(得分:10)

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

不使用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

Working Demo

请注意,在第二个版本中,我决定在分割之前不要乘以100。这需要强制转换为十进制,否则它将被隐式转换为int,导致没有返回记录

此外,我还不完全清楚你想要这个,但是你可以为这两个查询添加TOP 1,它会将结果限制为只有超过30%的商店数最多的那个

<强>更新

根据您的评论,这听起来是Kevin

  

你想要的行,从拥有最多员工的商店开始,直到你有至少30%

为止

这很困难,因为它需要一个运行百分比和一个bin包装问题,但这确实有效。注意我已经包含了另外两个测试用例(其中百分比完全相等,而且恰好超过了前两个)

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