我正在尝试将PERCENTILE_DISC与许多其他内置聚合函数一起使用。我尝试的代码是这样写的,但它失败了:
SELECT
[DataPoint] AS [DataPoint],
MIN([Value]) AS [Value MIN],
MAX([Value]) AS [Value MAX],
AVG([Value]) AS [Value AVG],
PERCENTILE_DISC(0.5)
WITHIN GROUP
(ORDER BY [Value])
OVER
(PARTITION BY [DataPoint])
AS MedianCont
FROM [Table] AS [Table]
WHERE ([DataPoint]
IN (
...
)
)
GROUP BY [DataPoint]
所以这有效......
SELECT
Distinct [DataPoint],
PERCENTILE_DISC(0.5)
WITHIN GROUP
(ORDER BY [Value])
OVER
(PARTITION BY [DataPoint])
AS MedianCont
FROM [Table] AS [Table]
WHERE ([DataPoint]
IN (
...
)
)
这有效......
SELECT
[DataPoint] AS [DataPoint],
MIN([Value]) AS [Value MIN],
MAX([Value]) AS [Value MAX],
AVG([Value]) AS [Value AVG]
FROM [Table] AS [Table]
WHERE ([DataPoint]
IN (
...
)
)
GROUP BY [DataPoint]
但是当我尝试将它们组合起来时,它希望我在Value
子句中声明Group By
,这是我不想要的,因为我想要一个明确的DataPoint
列表,不是每DataPoint
的值。
Column 'Value' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
答案 0 :(得分:5)
似乎使用over clause,从而创建了一个'窗口函数',将聚合函数与select语句分开。
SELECT
Distinct [DataPoint],
MIN([Value]) OVER (PARTITION BY [DataPoint]) AS [Value MIN],
MAX([Value]) OVER (PARTITION BY [DataPoint]) AS [Value MAX],
AVG([Value]) OVER (PARTITION BY [DataPoint]) AS [Value AVG],
PERCENTILE_DISC(0.5)
WITHIN GROUP
(ORDER BY [Value])
OVER
(PARTITION BY [DataPoint])
AS MedianCont
FROM [Table] AS [Table]
WHERE ([DataPoint]
IN (
...
)
)