包括分组查询的中位数。
Include Medians for grouped query .
我有这个查询需要绘制图表。
该图将显示每个位置的患者总数、患者最长等待时间(分钟)和患者等待时间中值。我需要在我的查询中包含中位数,这就是我遇到的问题。
示例原始数据:
到目前为止我有查询 w/o 中位数:
SELECT
[Location],
count([Patient_Number]) Total,
MAX([WaitTime_Min]) LongWait
FROM MyTable
where
[Location] in ('AMB', 'PEDS', 'WALK')
and [EDNurse] is NULL
group by [Location]
输出:
我需要帮助获取每个位置的 WaitTime 的最后一列中位数(来自原始数据)。任何帮助,将不胜感激;谢谢!!
期望的输出:
在 Sqlserver 2012 中有 PERCENTILE_CONT(for continues values), PERCENTILE_DISC(for discrete value)
方法可以做到这一点。你可以阅读更多相关信息 here.
SELECT
[Location],
count([Patient_Number]) Total,
MAX([WaitTime_Min]) LongWait,
max(MedianDisc) MedianDisc, -- Discrete median
max(MedianCont) MedianCont -- continues median
FROM (
select m.*, PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY WaitTime_Min)
OVER (PARTITION BY [Location]) AS MedianCont
,PERCENTILE_DISC(0.5) WITHIN GROUP (ORDER BY WaitTime_Min)
OVER (PARTITION BY [Location]) AS MedianDisc
from myTable m
where
[Location] in ('AMB', 'PEDS', 'WALK')
and [EDNurse] is NULL
)tt
group by [Location]
更新:
基于 Aaron 评论的此方法中的性能问题,我为中位数添加了一个纯 SQL 计算:
select [Location],
count([Patient_Number]) Total,
MAX([WaitTime_Min]) LongWait,
AVG(1.0 * LongWait) As Median
FROM
(
SELECT [Location],
[Patient_Number] Total,
[WaitTime_Min] LongWait
, ra=row_number() over (partition by [Location] order by [WaitTime_Min])
, rd=row_number() over (partition by [Location] order by [WaitTime_Min] desc)
from myTable m
where [Location] in ('AMB', 'PEDS', 'WALK')
and [EDNurse] is NULL
) as x
where ra between rd-1 and rd + 1
group by [Location]
我有这个查询需要绘制图表。
该图将显示每个位置的患者总数、患者最长等待时间(分钟)和患者等待时间中值。我需要在我的查询中包含中位数,这就是我遇到的问题。
示例原始数据:
到目前为止我有查询 w/o 中位数:
SELECT
[Location],
count([Patient_Number]) Total,
MAX([WaitTime_Min]) LongWait
FROM MyTable
where
[Location] in ('AMB', 'PEDS', 'WALK')
and [EDNurse] is NULL
group by [Location]
输出:
我需要帮助获取每个位置的 WaitTime 的最后一列中位数(来自原始数据)。任何帮助,将不胜感激;谢谢!!
期望的输出:
在 Sqlserver 2012 中有 PERCENTILE_CONT(for continues values), PERCENTILE_DISC(for discrete value)
方法可以做到这一点。你可以阅读更多相关信息 here.
SELECT
[Location],
count([Patient_Number]) Total,
MAX([WaitTime_Min]) LongWait,
max(MedianDisc) MedianDisc, -- Discrete median
max(MedianCont) MedianCont -- continues median
FROM (
select m.*, PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY WaitTime_Min)
OVER (PARTITION BY [Location]) AS MedianCont
,PERCENTILE_DISC(0.5) WITHIN GROUP (ORDER BY WaitTime_Min)
OVER (PARTITION BY [Location]) AS MedianDisc
from myTable m
where
[Location] in ('AMB', 'PEDS', 'WALK')
and [EDNurse] is NULL
)tt
group by [Location]
更新: 基于 Aaron 评论的此方法中的性能问题,我为中位数添加了一个纯 SQL 计算:
select [Location],
count([Patient_Number]) Total,
MAX([WaitTime_Min]) LongWait,
AVG(1.0 * LongWait) As Median
FROM
(
SELECT [Location],
[Patient_Number] Total,
[WaitTime_Min] LongWait
, ra=row_number() over (partition by [Location] order by [WaitTime_Min])
, rd=row_number() over (partition by [Location] order by [WaitTime_Min] desc)
from myTable m
where [Location] in ('AMB', 'PEDS', 'WALK')
and [EDNurse] is NULL
) as x
where ra between rd-1 and rd + 1
group by [Location]