一个简单的查询:
var viewModel = {
dataSource: Application_with_Chart.db.nom_clasa_articole.toDataSource({
select: ["id","product", "quantity"]
})
};
return viewModel;
在SQL中:
SELECT id,product,quantity
FROM nom_clasa_articole
这很简单,但我需要一个带有group-by和sum:的查询
SELECT id, product, SUM(quantity)
FROM nom_clasa_articole
GROUP BY product
如何在devexpress中编写此查询?
如果我没有错的话,问题是关于DevExtreme多通道应用程序。他们使用OData服务。
OData prodocol没有SQL GROUP BY
的直接等价物。这里有两个选项:
-
在服务器端使用OData服务操作进行分组。
-
在
postProcess
处理程序中对客户端执行分组,如下所示。
客户端分组:
var viewModel = {
dataSource: Application32.db.nom_clasa_articole.toDataSource({
select: ["id", "product", "quantity"],
postProcess: function(items) {
// Grouping occurs here
var groups = {};
$.each(items, function() {
var groupKey = this.product;
if(!groups[groupKey])
groups[groupKey] = { quantity: 0 };
groups[groupKey].quantity += this.quantity;
});
return $.map(groups, function(v, k) {
return { product: k, quantity: v.quantity };
});
}
})
};