Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SQL Server Analytic functions #28037

Open
1 of 2 tasks
popcatalin81 opened this issue May 16, 2022 · 1 comment
Open
1 of 2 tasks

SQL Server Analytic functions #28037

popcatalin81 opened this issue May 16, 2022 · 1 comment

Comments

@popcatalin81
Copy link

popcatalin81 commented May 16, 2022

Add support for SQL Server Analytic Functions

Currently the only supported function is ROW_NUMBER but even this function is not supported directly, it's only used internally by the Query pipeline in certain scenarios.

For example PERCENTILE_CONT or PERCENTILE_DISC are analytic functions which can be used to calculated the median of a dateset. An operation which is overly complicated using other query methods.

Example 1 of Median calculation with Pure SQL:

SELECT
(
 (SELECT MAX(Score) FROM
   (SELECT TOP 50 PERCENT Score FROM Posts ORDER BY Score) AS BottomHalf)
 +
 (SELECT MIN(Score) FROM
   (SELECT TOP 50 PERCENT Score FROM Posts ORDER BY Score DESC) AS TopHalf)
) / 2 AS Median

Example 2 of Median calculation with PERCENTILE_CONT

SELECT TOP 1 PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY Score) OVER (PARTITION BY null) As Median FROM Posts 

The first query is very hard to translate into Linq (As SELECT TOP PERCENT is not supported). It requires to query the count first, then query the max value from top half and the min value from bottom half and average them)

Desired solution:

Accessing the SQL Server Analytic functions could be done using the EF.Functions extensions:

Example:

db.Posts.Select(x => EF.Functions.PercentileCont(0.5, x.Score, null)).First();

The above Linq expression would translate to Example 2 SQL.

Dependencies:

@roji
Copy link
Member

roji commented May 16, 2022

Translating to PERCENTILE_CONT should be partially possible once we have support for custom provider aggregate functions (#22957). The partition notably wouldn't be covered (this is probably related to window functions, #12747).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants