在Oracle中运行良好。。。。。。
oracle查询如下
Select distinct channel_id, position_id,datamonth,
percentile_cont(.9) within group (order by TRIM_PRE_ELIG_PAY)
over (partition by channel_id, position_id, datamonth) as TRIM_PRE_ELIG_PAY_90th_PERC
from Tablename
但对于SQL Server,我遇到了一个错误。以下是SQL Server 2008:的查询
Select
distinct channel_id,
position_id, datamonth,
percentile_cont(.9) within group (order by TRIM_PRE_ELIG_PAY)
over (partition by channel_id) as TRIM_PRE_ELIG_PAY_90th_PERC
from table
错误:无法正确分析Select。输出不能为生成。
我知道它可以在SQL Server 2012中正常工作,但在SQL Server 2008 中需要一种替代方法
有人能帮忙吗。。。。。。。。。。。
SQL Server Engine博客上有一个适用于SQL Server 2005+的解决方法
不幸的是,它很长很复杂:我会把链接留给你,而不是试图根据你的查询进行调整。。。
您可以创建一个CLR聚合函数来实现它。唯一的失败是,您将不得不重新安排您的查询。我使用CLR实现了percentile_cont。请阅读此处,了解如何创建CLR。然后,您可以使用此代码来获得与percentile_cont相同的O/P。它比编写多个语句容易得多。
你肯定可以根据你的使用情况对它进行一些改进/调整。
using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
using System.Collections.Generic;
[Serializable]
[Microsoft.SqlServer.Server.SqlUserDefinedAggregate(
Format.UserDefined,
IsInvariantToDuplicates = false,
IsInvariantToNulls = false,
IsInvariantToOrder = false,
MaxByteSize = 8000)]
public struct Percentile_Cont : IBinarySerialize
{
//Variables to hold the values;
private List<decimal> _list;
private decimal _percentile;
public void Init()
{
_list = new List<decimal>();
_percentile = new decimal();
}
public void Accumulate(SqlDecimal value,SqlDecimal percentile)
{
if (!value.IsNull)
{
_list.Add(value.Value);
_percentile = (decimal)percentile;
}
}
///
/// Merge the partially computed aggregate with this aggregate.
///
/// The other partial results to be merged
public void Merge(Percentile_Cont group)
{
this._list.AddRange(group._list.ToArray());
}
///
/// Called at the end of aggregation, to return the results.
///
/// The percentile of all inputted values
public SqlDecimal Terminate()
{
if (_list.Count == 0)
return SqlDecimal.Null;
_list.Sort();
if (_percentile < 0 || _percentile >= 1)
return SqlDecimal.Null;
var index =
(int) Math.Ceiling
(_percentile * _list.Count + 0.5m);
if(index > _list.Count)
{
index = index - 1;
}
return _list[index-1];
}
#region IBinarySerialize Members
public void Read(System.IO.BinaryReader binaryReader)
{
int cnt = binaryReader.ReadInt32();
this._list = new List<decimal>(cnt);
this._percentile = new decimal();
for (int i = 0; i < cnt; i++)
{
this._list.Add(binaryReader.ReadDecimal());
}
this._percentile = binaryReader.ReadDecimal();
}
public void Write(System.IO.BinaryWriter binaryWriter)
{
binaryWriter.Write(this._list.Count);
foreach (decimal d in this._list)
{
binaryWriter.Write(d);
}
binaryWriter.Write(_percentile);
}
#endregion
}