首页 > 解决方案 > SQL Server SQL Query中的Excel四分位函数

问题描述

我有一组这样的值:

40
50
50
66
83
100
100
100
100
100
100
100
100
100
100
100
100

当我在 excel 中执行四分位数函数时,我得到了我的第一个四分位数(25)、第二个四分位数(50)、第三个四分位数(75)和最大值(100)的这四个值

四分位数 = 83.33、100、100、100

因此,当我比较一个获得 80% 的销售代表时,根据 excel 计算,他们将落在最后四分之一。

我需要在 sql 中重做相同的功能,并在下面给出了我的代码。

declare @sales table(
salesRepId int,
percentageSales int)

insert into @sales(salesRepId, percentageSales)
values(1,40)
,(2,50)
,(3,50)
,(4,66.7)
,(5,83.33)
,(6,100)
,(7,100)
,(8,100)
,(9,100)
,(10,100)
,(11,100)
,(12,100)
,(13,100)
,(14,100)
,(15,100)
,(16,100)
,(17,100);

with quintile as(
            select percentagesales, ntile(4) over(order by percentagesales) 
as quintile
            from (select distinct percentagesales from @sales) as s
            )
            select salesrepid, r.percentagesales, q.quintile
            from @sales r
            join quintile q on r.percentagesales = q.percentagesales
            order by q.quintile, percentagesales

当我运行它时,我得到以下结果集: 查询结果

salesrepid  percentagesales quintile
1   40  1
2   50  1
3   50  1
4   66  2
5   83  3
6   100 4
7   100 4
8   100 4
9   100 4
10  100 4
11  100 4
12  100 4
13  100 4
14  100 4
15  100 4
16  100 4
17  100 4

根据sql,80%会落在中间四分位。

如何在 SQL 查询中获取类似于 excel 的四个百分位值

标签: sql-serverquartile

解决方案


我尝试了 percentile_cont 建议的 Larnu,我得到的四个四分位数为

83,100,100,100

但从技术上讲,它应该是 83.33,100,100,100

这是我到目前为止的查询

declare @sales table(
salesRepId int,
percentageSales int)

insert into @sales(salesRepId, percentageSales)
values(1,40.00)
,(2,50.00)
,(3,50.00)
,(4,66.77)
,(5,83.33)
,(6,100.00)
,(7,100.00)
,(8,100.00)
,(9,100.00)
,(10,100.00)
,(11,100.00)
,(12,100.00)
,(13,100.00)
,(14,100.00)
,(15,100.00)
,(16,100.00)
,(17,100.00);

with quintile as(
            select percentagesales, ntile(100) over(order by percentagesales) as quintile
            from (select distinct percentagesales from @sales) as s
            )
            select salesrepid, r.percentagesales, q.quintile
            from @sales r
            join quintile q on r.percentagesales = q.percentagesales
            order by q.quintile, percentagesales;

declare @p Decimal(2,2) = 0.25;
    with quartile as(
            select 
            salesRepId,
            percentageSales 
            ,cast(percentile_cont(.25)  within group(order by percentagesales) over() as decimal(36,2)) as quartile25th
            ,cast(percentile_cont(.5)  within group(order by percentagesales) over() as decimal(36,2)) as quartile50th
            ,cast(percentile_cont(.75)  within group(order by percentagesales) over() as decimal(36,2)) as quartile75th
            ,cast(percentile_cont(1)  within group(order by percentagesales) over() as decimal(36,2)) as quartile100th
            from @sales
            )
            select 
                s.salesRepId
                ,s.percentageSales
                , case 
                    when s.percentageSales < q.quartile25th then 'red'
                    when s.percentageSales >=q.quartile25th and s.percentageSales <q.quartile75th then 'yellow'
                    when s.percentageSales >= q.quartile75th then 'green'
                 end as color

            From @sales s
            join quartile q on q.percentageSales = s.percentageSales and q.salesRepId = s.salesRepId

我如何像在 excel 中一样获得四分位数值。

谢谢


推荐阅读