分区中位数

时间:2017-11-29 09:00:57

标签: sql sql-server tsql median

我想知道是否有人可以提供帮助我正在尝试计算在我看来遵守分组的中位数。

我喜欢以下代码,但它只是给了我每行中的所有中位数。我想我需要使用OVER(PARTITION BY()),但即使经过谷歌搜索和阅读像这样的着名文章https://sqlperformance.com/2012/08/t-sql-queries/median

,我也无法做出头脑或尾巴。
 `SELECT
 YEAR(reportsubmitted) as “Year Submitted”,
 Month(reportsubmitted) as “Month Submitted”, COUNT (DISTINCT(propertyid)) as 
 “Number of Reports Submitted”, SUM([report fee]) as “Total Report Fee”,

(
(SELECT MAX([days From Audit to Submission])

FROM (SELECT TOP 50 PERCENT ([days From Audit to Submission] )

FROM vwCMnAuditorsProcessLength WHERE ReportSubmitted > ‘2017-04-01’ ORDER BY 
[days From Audit to Submission] ) AS x)

(SELECT MIN([days From Audit to Submission])

FROM (SELECT TOP 50 PERCENT [days From Audit to Submission]
FROM vwCMnAuditorsProcessLength WHERE ReportSubmitted > ‘2017-04-01’ ORDER BY 
[Report Fee] DESC) AS y)  
) / 2.0 as “Median Days”

FROM vwCMnAuditorsProcessLength
WHERE reportsubmitted >= ‘2017-04-01’

GROUP BY MONTH(reportsubmitted), YEAR(reportsubmitted)`

我确实尝试了以下不同的东西,但似乎是在打折很多数据

SELECT

[MMYYYY ReportSubmitted],

[Total Report Fee],

[Number of Reports Submitted],

AVG([days from audit to submission]) as “Median days to Submission”

FROM (

SELECT [MMYYYY ReportSubmitted], [report fee], propertyid,
CAST([days from audit to submission] as decimal(5,2)) [days from audit to submission],

ROW_NUMBER() OVER(
Partition by [MMYYYY ReportSubmitted]
Order by [days from audit to submission] ASC) AS “RowASC”,

ROW_NUMBER() OVER(
Partition by [MMYYYY ReportSubmitted]
Order by [days from audit to submission] DESC) AS “RowDESC”,

SUM([report fee]) OVER(Partition by [MMYYYY ReportSubmitted] Order by [days from 
 audit to submission]) AS “Total Report Fee”,
COUNT(propertyid) OVER(Partition by [MMYYYY ReportSubmitted] Order by [days from audit to submission]) AS “Number of Reports Submitted”

FROM vwCMnAuditorsProcessLength) x

WHERE RowASC in (RowDESC,RowDESC-1,RowDESC+1)

 Group by [MMYYYY ReportSubmitted], [Total Report Fee], [Number of Reports Submitted]
Order by [MMYYYY ReportSubmitted]

如果有人有任何想法,我会非常感激

0 个答案:

没有答案