When we use SQL Server, we can easily pass the results through SUM, AVER, COUNT, etc., then in the DataSet (DataTable) that has been retrieved? Especially through the Web Service, I got DataSet, this time, there is no way to modify the SELECT statement to get these statistics. So is it possible to count in DataSet / DataTable? The answer is yes.
ID = "AD_TOP" name = "ad_top" align = "left" marginwidth = "0" marginheight = "0" src = "http://adv.pconline.com.cn/adpuba/show?id=pc.rjzx. Kaifa.wenzhang.hzh. & media = HTML & PID = cs.pconline.rjzx.hzh. "Frameborder =" 0 "width =" 320 "scrolling =" no "height =" 280 "> In MSDN, there is a MS recommended statistics Method, it is to sum up the data on the data. This method is actually equal to no (perhaps this method is only for DataGrid to obtain small counts), because this method is used by DataGrid's itemDataBind events to do Tired, there is no difference between our manual writing code statistics.
This article describes a simple method that does not require a record-by-frame recording to easily get the record statistics in DataTable. This simple way is to call the function of powerful DataTable.
First, call instructions (as an example, below):
Public Object Compute (String Strfilter), STRING STRECT COMPUTE
parameter:
Strexpression: The expression string to calculate, basically similar to statistical expressions in SQL Server
Strfilter: Statistical filter string, only records that satisfy this filter condition will be statistics
Second, call example:
The following example, suppose a product sales table table describes the actual records of each promoter sales in a shopping mall, including the field as: name (name), gender (SEX, 0 is female, 1 is male), birthday (birthday), sales The product's code (ProID), the number of sales (Quantity), Sales Price.
1. The quantity of the salesperson for all genders:
Table.Compute ("count (*)", "sex = 0");
2. Statistics All salespersons are greater than 20 years old.
Table.Compute ("count (*)", "birthday <'" today); // Today is today's date string
3. Average price of statistical sales products
Table.Compute ("AVER (Price)", "True");
4. Product sales quantity of statistical product code:
Table.Compute ("QUM (Quantity", "ProID = 1"); 5. Statifying the total amount of sales:
To count the total sales amount, because there is no product data for a certain product sales in Table, we can get it through Quantity * Price. such as:
Table.Compute ("Quantity * Price", "True");
One problem here is that the statistical function of DataTable has no SQL Server, which is wrong because Compute does not have SUM (Quantity * Price). then what should we do?
For such complex data statistics, we can create a new field in DataTable to complete, such as Amount, and set the expression of this field for Quantity * Price, so we can use the statistics:
Table.Compute ("SUM (AMOUNT)", "True");