You are on page 1of 13

Modern Programming Language

Lecture-05

Turning Data into Information


Agenda
Return

a value that aggregates data from a table

column
Add

a column that aggregates data from a table, or from its parent or child table

Build

an index-based view of a table

Generate

a new table based on a projected view of the original table

Aggregating Data
An

aggregation function returns a single calculated value from a set of related values. ADO.NET includes seven aggregation functions for use in expression columns and other DataTable features. Calculates the total of a set of column values. The column being summed must be numeric, either integral or decimal. : Returns the average for a set of numbers in a column. This function also requires a numeric column.

Sum:

Avg

Cont

Min: Indicates the minimum value found within a set of column values. Numbers, strings, dates, and other types of data that can be placed in order are all valid for the target column.

Max: Like Min, but returns the largest value from the available column values. As with the Min function, most column types will work.
Count: Simply counts the number of rows included in the aggregation. You can pass any type of column to this function. As long as a row includes a non-NULL value in that column, it will be counted as 1. StDev: Determines the statistical standard deviation for a set of values, a common measure of variability within such a set. The indicated column must be numeric. Var: Calculates the statistical variance for a set of numbers, another measurement re-lated to the standard deviation. Only numeric columns are supported.

Dim employees As New DataTable("Employee") employees.Columns.Add("ID", GetType(Integer)) employees.Columns.Add("Gender", GetType(string)) employees.Columns.Add("FullName", GetType(String)) employees.Columns.Add("Salary", GetType(Decimal)) ' ----- Add employee data to table, then... Dim averageSalary As Decimal = CDec(employees.Compute("Avg(Salary)", ""))

The Compute method calculates the average of the values in the Salary column. The second argument to Compute is a flter that limits the rows included in the calculation.

Dim femalesInCompany As Integer = CInt(employees.Compute("Count(ID)", "Gender = 'F'"))

Adding an Aggregate Column


Dim sports As New DataTable("Sports") sports.Columns.Add("SportName", GetType(String)) sports.Columns.Add("TeamPlayers", GetType(Decimal)) sports.Columns.Add("AveragePlayers", GetType(Decimal), "Avg(TeamPlayers)")

sports.Rows.Add({"Baseball", 9}) sports.Rows.Add({"Basketball", 5}) sports.Rows.Add({"Cricket", 11})

MessageBox.Show(CStr(sports.Rows(0)!AveragePlayers)) ' Displays 8.3... MessageBox.Show(CStr(sports.Rows(1)!AveragePlayers)) ' Also 8.3...

Aggregating Data Across Related Tables


' ----- Build the parent table and add some data. Dim customers As New DataTable("Customer") customers.Columns.Add("ID", GetType(Integer)) customers.Columns.Add("Name", GetType(String)) customers.Rows.Add({1, Ali"}) customers.Rows.Add({2, Khan"})

' ----- Build the child table and add some data. The "Total" ' expression column adds sales tax to the subtotal.

Dim orders As New DataTable("Order")


orders.Columns.Add("ID", GetType(Integer)) orders.Columns.Add("CustomerID", GetType(Integer)) orders.Columns.Add("Subtotal", GetType(Decimal)) orders.Columns.Add("TaxRate", GetType(Decimal)) orders.Columns.Add("Total", GetType(Decimal), "Subtotal * + TaxRate")

' ----- Two sample orders for customer 1, 1 for customer 2. orders.Rows.Add({1, 1, 35.24, 0.0875}) ' Total = $38.32 orders.Rows.Add({2, 1, 56.21, 0.0875}) ' Total = $61.13 orders.Rows.Add({3, 2, 14.94, 0.0925}) ' Total = $16.32

Cont
' ----- Link the tables within a DataSet. Dim business As New DataSet business.Tables.Add(customers)

business.Tables.Add(orders)
business.Relations.Add(customers.Columns!ID, orders.Columns!CustomerID)

' ----- Here is the aggregate expression column.

customers.Columns.Add("OrderTotals", GetType(Decimal), "Sum(Child.Total)")

' ----- Display each customer's order total. For Each scanCustomer As DataRow In customers.Rows
TextBox1.Text &= (CStr(scanCustomer!Name) & ": " & scanCustomer!OrderTotals & vbCrLf)

Next scanCustomer

Setting Up Indexed Views


The

DataTable.Select method lets you apply a selection query to a table, returning a subset of the available rows in the DataTable.

Its

convenient, but if you will run the same query against the table repeatedly, its not the most efficient use of computing resources.

DataView class
ADO.NET

includes the DataView class. As with the DataTable class, each DataView exposes a set of DataRow objects. But unlike the DataTable, the DataView does not actually contain any DataRow instances. Instead, it contains an index that refers to rows in a true DataTable. builds this index using the same query expressions used by the DataTable.Select method, with support for both a row selection component and a sorting component.

It

Creating a DataView
To

create a DataView from a DataTable, pass the table to the DataView constructor.

Dim someView As New DataView(someTable)


The To

new view includes all the rows in the original table, sorted in the order they appear in the DataTable. alter the included rows, set the DataView objects RowFilter property.

Dim managersOnly As New DataView(employees) managersOnly.RowFilter = "IsManager = True"


To

sort the views rows, set the DataView.Sort property,

managersOnly.Sort = "HireDate DESC"

Using a DataView
The

DataView class includes several features that return information about the in-view rows. The most basic is the DataView.Count property

Dim managersOnly As New DataView(employees) managersOnly.RowFilter = "IsManager = True" MessageBox.Show("Total Managers: " & managersOnly.Count)

Questions???

You might also like