Aliased Column Enhancements
When you use the SQL "Over" syntax you need to give the derived column an alias in order to be able to use it later in your query. This is now supported by EntitySpaces. Notice the two esQueryItem variables at the beginning the source code below. These are provided to the .As() method as "out" parameters and used later throughout the query. For instance, notice how "rowNumber" is created in the nested query and then used in the outer Select() and the outer Where() clause as well.
SQL 'Over' Syntax Now Supported
Notice the Over.RowNumber() column in the nested query's Select() clause. We need to use this derived column later in the outer Select() and Where() clauses. Thus, we use the 'out' parameter in the .As() so we can use the aliased column later in our query.
C# Query Example
This query doesn't really make sense though it works, it's just to demonstrate the syntax.
esAlias rowNumber = null;
OrdersCollection coll = new OrdersQuery("o", out var o)
.From<OrderDetailsQuery>(out var od, () =>
{
// Nested Query
return new OrderDetailsQuery("od", out var subQuery)
.Select
(
subQuery.Over.RowNumber().OrderBy(subQuery.OrderID.Descending).As("RowNumber", out rowNumber)
)
.GroupBy(subQuery.OrderID);
}).As("sub")
.Select(rowNumber())
.Where(rowNumber() > 5)
.ToCollection<OrdersCollection>();
if (coll.Count > 0)
{
// Then we loaded at least one record
}
The SQL Generated by the Query
Notice that the SQL generated by EntitySpaces is exactly what you'd expect. The columns alias 'sub' is used where needed only.
Resulting SQL Query
SELECT sub.[RowNumber]
FROM
(
SELECT ROW_NUMBER() OVER( ORDER BY od.[OrderID] DESC ) AS 'RowNumber'
FROM [Order Details] od
GROUP BY od.[OrderID]
)
AS sub
WHERE sub.[RowNumber] > @Expr1
List of Supported SQL Over Functions
These are the SQL Over functions supported by EntitySpaces.
See this link for more information => SELECT - OVER Clause (Transact-SQL).
These enhancements are initially coming for Microsoft SQL Server but will be added to the rest of the providers shortly.
Ranking Over Functions
- DENSE_RANK() OVER
- NTILE() OVER
- RANK() OVER
- ROW_NUMBER() OVER
Aggregate Over Functions
- COUNT() OVER
- COUNT_BIG() OVER
- AVG() OVER
- MIN() OVER
- MAX() OVER
- SUM() OVER
- STDDEV() OVER
- STDDEVP() OVER
- VAR() OVER
- VARP() OVER
Analytical Over Functions
- LAG() OVER
- LEAD() OVER
- CUM_DIST() OVER
- FIRST_VALUE() OVER
- LAST_VALUE() OVER
- PERCENTILE_CONT() OVER
- PERCENTILE_DISC() OVER
This query, although not realistic, demonstrates the syntax in a real live working query ...
OrdersCollection coll = new OrdersQuery("q", out var q)
.Select
(
q.Over.RowNumber().OrderBy(q.EmployeeID.Descending).As("Alias1"),
q.Over.RowNumber().PartitionBy(q.Freight.Sum() * 10).OrderBy(q.EmployeeID.Descending).As("Alias2"),
q.Over.Rank().OrderBy(q.EmployeeID.Descending).As("Alias3"),
q.Over.DenseRank().OrderBy(q.EmployeeID.Descending).As("Alias4"),
q.Over.Ntile(4).OrderBy(q.EmployeeID.Descending).As("Alias5"),
q.Over.Sum(q.Freight).PartitionBy(q.EmployeeID).OrderBy(q.EmployeeID.Descending).As("Alias6")
)
.GroupBy(q.EmployeeID, q.Freight)
.ToCollection();
This functionality will be released in the next 10 days or so. Stay tuned. See EntitySpaces for more information.
Hello Mike. i'm a big fan of your work. especially entityspaces since the very starting project.
ReplyDeleteSorry, I'm not shure this is the best place to ask you a question, but a didn't find another way to join you.
I just went to discover the new version and I certainly missed something, I didn't see how to use entityspaces configsection in web.config tu declare connectionstrings. in the previous version all work fine, in the new one, i must adding manualy connection in esConfigSettings.ConnectionInfo.Connections at the application start. please, could you tell me where i'm wrong please ?