How to query summary/group in devexpress with advanced queries

565 views Asked by At

A simple query:

var viewModel = {
    dataSource: Application_with_Chart.db.nom_clasa_articole.toDataSource({
        select: ["id","product", "quantity"]
    })
};

return viewModel;

in SQL:

SELECT id,product,quantity
FROM nom_clasa_articole

This is simple, but I need a query with group by and sum:

SELECT id, product, SUM(quantity)
FROM nom_clasa_articole
GROUP BY product

How can I write this query in devexpress?

1

There are 1 answers

1
amartynov On

If I am not mistaken, the question is about DevExtreme Multi-channel applications. They use OData services.

OData prodocol does not have direct equivalent to SQL GROUP BY. You have two options here:

  1. Use OData service operations for grouping on the server side.

  2. Perform grouping on the client in a postProcess handler as shown below.

Client-side grouping:

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 }; 
            });
        }
    })
};