我正在努力找出使用' over clause'的最佳方法。使用sequelize.fn
。
我的原始查询可能是这样的:
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 (
...
)
)
对于MIN
,MAX
,AVG
,我可以构建一个看起来像这样的对象数组:
[sequelize.fn(type, sequelize.col('table.col')), 'colName']
与我的不同数据点的逐个对象组合。
但如果我还包含PERCENTILE_DISC
语法,我需要使用窗口函数,否则会出现not contained in either an aggregate function or the GROUP BY clause
错误。
不确定sequelize方法是否允许我以这种方式构造查询(缺少编写原始查询)。如果我确实需要原始查询,是否可以只对PERCENTILE_DISC
部分进行原始查询?