【发布时间】:2020-08-08 06:20:33
【问题描述】:
我正在尝试计算每个类别中交易的中位数。 一些注意事项(因为下面的数据集是一个更大数据集的一个小 sn-p):
- 一个员工可以属于多个类别
- 每笔交易的中位数应 > 0
- 并非每个人都出现在每个类别中
数据是这样设置的:
| Person | Category | Transaction |
|:-------:|:--------:|:-----------:|
| PersonA | Sales | 27 |
| PersonB | Sales | 75 |
| PersonC | Sales | 87 |
| PersonD | Sales | 36 |
| PersonE | Sales | 70 |
| PersonB | Buys | 60 |
| PersonC | Buys | 92 |
| PersonD | Buys | 39 |
| PersonA | HR | 59 |
| PersonB | HR | 53 |
| PersonC | HR | 98 |
| PersonD | HR | 54 |
| PersonE | HR | 70 |
| PersonA | Other | 46 |
| PersonC | Other | 66 |
| PersonD | Other | 76 |
| PersonB | Other | 2 |
理想的输出应该是这样的:
| Category | Median | Average |
|:--------:|:------:|:-------:|
| Sales | 70 | 59 |
| Buys | 60 | 64 |
| HR | 59 | 67 |
| Other | 56 | 48 |
我可以通过以下方式获得平均值:
SELECT
Category,
AVG(Transaction) AS Average_Transactions
FROM
table
GROUP BY
Category
效果很好!
This post 试图帮助我找到中位数。我写的是:
SELECT
Category,
PERCENTILE_DISC(0.5) WITHIN GROUP (ORDER BY Transaction) OVER (PARTITION BY Category) AS Median_Transactions
FROM
table
GROUP BY
Category
但我得到一个错误:
Msg 8120: Column 'Transactions' is invalid in the select list because it is not contained in either an aggregate function or the **GROUP BY** clause
我该如何解决这个问题?
【问题讨论】:
标签: sql sql-server aggregate-functions median