我有一个查询OData端点的要求,以返回由Status分组的行以及列数据上的聚合函数。例如,返回迄今付款少于100的所有发票的总数,并返回迄今付款少于500的所有发票的总数。
我的数据模型具有以下字段(通过OData API公开的C#模型)。
public class Invoice
{
public string Status {get; set;}
public string InvoiceType {get; set;}
public decimal InvoicePayments {get; set;}
public decimal EligibleValue {get; set;}
}
通过执行以下查询,可以在SQL查询级别获取所需的数据:
SELECT
[i].[Status],
COUNT(*) AS [Count],
(SELECT SUM(t.val) FROM (SELECT InvoicePurchaseValue as val FROM Invoice WHERE InvoicePayments < 100) t) AS ValueLessThan100,
(SELECT SUM(t.val) FROM (SELECT InvoicePurchaseValue as val FROM Invoice WHERE InvoicePayments < 500) t) AS ValueLessThan500
FROM [Invoice] AS [i]
GROUP BY [i].[Status]
但是我一辈子都想不通如何通过单个OData查询来做到这一点。
我已经了解了
http://<myserver>/odata/Invoice?$apply=groupby((Status),aggregate(InvoicePurchaseValue with sum as ValueLessThan100, InvoicePurchaseValue with sum as ValueLessThan500))
但是我不知道如何在每一列上执行子查询。