获取间隔百分比

时间:2017-03-08 05:31:58

标签: sql sql-server group-by sql-server-2014

请考虑此表:

FileName            FileSize
----------------------------
 1                   33
 2                   198
 3                   10
 4                   127
 5                   85
 6                   23
 7                   105
 8                   158
 9                   78
 10                  90

我想创建这样的结果:

FileSize             Percentage
--------------------------------
1-50                    30%
50-150                  50%
150-200                 20%

我如何group by并根据间隔创建百分比?

由于

4 个答案:

答案 0 :(得分:2)

对于没有边界表的查询,您可以使用VALUES构造函数(如下面的

)创建内联表
select DISTINCT
CAST(I.LowNumber as VARCHAR) + ' - '+  CAST(I.HighNumber as VARCHAR) as FileSize,
COUNT(*) OVER (PARTITION BY lowNumber,HighNumber ORDER By lowNumber) * 100.00 /COUNT(*) OVER ( ORDER BY (SELECT 1)) as percentage
from TblFile F
join (values (1, 50),(50, 150),(150, 200)) as I(LowNumber, highNumber)
on F.FileSize >=I.LowNumber and F.FileSize<I.HighNumber
Order By I.LowNumber

您的查询应该是

select DISTINCT
CAST(I.LowNumber as VARCHAR) + ' - '+  CAST(I.HighNumber as VARCHAR) as FileSize,
COUNT(*) OVER (PARTITION BY lowNumber,HighNumber ORDER By lowNumber) * 100.00 /COUNT(*) OVER ( ORDER BY (SELECT 1)) as percentage
from TblFile F
join TblInterval I 
on F.FileSize >=I.LowNumber and F.FileSize<I.HighNumber

<强>解释

理想情况下,您应该利用基于集合的方法并将范围值存储在表格中。这样可以加快处理速度,并且允许您在一个地方限制外部而不是在程序中。这也符合依赖注入原则。

对于内联匿名表,使用VALUES构造函数。有关详情,请参阅this msdn link

<强> Output image reference:

PS:为表格插入脚本

--create table tblInterval (LowNumber Int, HighNumber Int)
--insert into tblInterval values
--(1,50),(50,150),(150,200)

create table tblFile (fileName int,fileSize int)
insert into tblFile values
( 1 ,33)
,( 2 ,198 )
,( 3 ,10  )
,( 4 ,127 )
,( 5 ,85  )
,( 6 ,23  )
,( 7 ,105 )
,( 8 ,158 )
,( 9 ,78  )
,( 10,90  )

假设你有一个如下表

TblInterval

LowNumber HighNumber
  1        50
 50       150
150       200

答案 1 :(得分:1)

您可以使用case语句来设置文件大小范围,然后按计数获得百分比

select a.range as FileSize, (Count(*)* 100 / (Select Count(*) From MyTable)) as Percentage
from (
select case  
when FileSize between 1 and 50 then '1-50'
when FileSize between 50 and 150 then '50-150'
when FileSize between 150 and 200 then '150-200' end as range
from MyTable) a
group by a.range

答案 2 :(得分:1)

CREATE TABLE #A
(
FILENAME   INT,         FILESIZE INT
)
INSERT INTO #A VALUES
(1,33),
(2,198),
(3,10),
(4,127),
(5,85),
(6,23),
(7,105),
(8,158),
(9,78),
(10,90)

SELECT RANGE,COUNT(*)*100/(SELECT COUNT(*) FROM #A)    AS PERCENTAGE
FROM ( 
SELECT *,CASE  
WHEN FILESIZE BETWEEN 1 AND 50 THEN '1-50'
WHEN FILESIZE BETWEEN 50 AND 150 THEN '50-150'
WHEN FILESIZE BETWEEN 150 AND 200 THEN '150-200' END AS RANGE
FROM #A) A
GROUP BY A.RANGE
ORDER BY CASE WHEN RANGE = '1-50' THEN 1 
              WHEN RANGE ='50-150' THEN 2
              WHEN RANGE ='150-200' THEN 3
                                    END 

输出

RANGE   PERCENTAGE
1-50    30
50-150  50
150-200 20

答案 3 :(得分:0)

您可以将AVG与分区一起使用:

select distinct 
case  
when FileSize between 1 and 50 then '1-50'
when FileSize between 50 and 150 then '50-150'
when FileSize between 150 and 200 then '150-200' end as range
, avg(FileSize) OVER (PARTITION BY (select 
                                    case when filesize between 0 and 50 then 1 
                                    when filesize between 50 and 150 then 2
                                    when filesize between 150 and 200 then 3
                                    end)) as percentage
from mytable