1

在 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

错误:无法正确解析选择。无法生成输出。

我知道它可以在 SQL Server 2012 中正常工作,但在 SQL Server 2008 中需要另一种方法

任何人都可以帮助............

4

2 回答 2

2

SQL Server 引擎博客上有一个适用于 SQL Server 2005+的解决方法

不幸的是,它很长而且令人费解:我会给你留下链接,而不是尝试调整它以适应你的查询......

于 2012-02-14T12:29:07.817 回答
1

您可以创建一个 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
}
于 2012-04-23T14:27:11.077 回答