1

In Oracle it works well ......

Query for oracle is As Follows

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

But for SQL Server, I'm getting an error. Here's the query for 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

ERROR: Select could not be parsed correctly. Output could not be generated.

I got to know that it can work properly in SQL Server 2012 but need an alternative way in SQL Server 2008

Can anybody help...........

Aaron Bertrand
  • 272,866
  • 37
  • 466
  • 490

2 Answers2

2

There is a workaround on the SQL Server Engine blog that applies to SQL Server 2005+

Unfortunately, it's quite long and convoluted: I'll leave you with the link rather than attempt to adapt it for your query...

gbn
  • 422,506
  • 82
  • 585
  • 676
1

You can create a CLR Aggregate function to implement the same. The only downfall is that you will have to re-arrange your query a bit. I implemented the percentile_cont using the CLR. Read here on how to create the CLR . Then you can use this code to get the same O/P as percentile_cont. Its a lot more easier than writing multiple statements.

You can definitely refine/tweak it a bit depending on your usage.

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
}
Egalitarian
  • 2,168
  • 7
  • 24
  • 33