-sql native
SELECT *, count(*) FROM `tbl_order_detail` a inner join tbl_order b on a.or_id=b.or_id group by a.or_id
- Linq With Aggregate
Imports System
Imports System.Linq
Imports System.Linq.Expressions
Imports System.Collections.Generic
Imports System.Data
Imports System.Data.Odbc
Imports System.Data.Common
Imports System.Globalization
Public Class Form1
Dim conn As OdbcConnection
Dim comm As OdbcCommand
Private Sub connect1()
conn = New OdbcConnection("dsn=posdb_cafe;uid=root")
conn.Open()
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim ds As New DataSet()
ds.Locale = CultureInfo.InvariantCulture
' See the FillDataSet method in the Loading Data Into a DataSet topic.
connect1()
Dim sqlStr As String = "select * from tbl_item"
comm = New OdbcCommand(sqlStr, conn)
Dim myDA As OdbcDataAdapter = New OdbcDataAdapter(comm)
'conn.Close()
myDA.Fill(ds, "tbl_item")
Dim products As DataTable = ds.Tables("tbl_item")
Dim query = From product In products.AsEnumerable() Where (product.Field(Of String)("item_name")) = "Beef" _
Or (product.Field(Of String)("item_name")) = "99"
Select product
Console.WriteLine("Product Names:")
For Each p In query
MsgBox(p.Field(Of String)("item_name"))
Next
''''''''''''''
Dim sqlStr2 As String = "select * from tbl_order"
comm = New OdbcCommand(sqlStr2, conn)
Dim myDA2 As OdbcDataAdapter = New OdbcDataAdapter(comm)
myDA2.Fill(ds, "tbl_order")
Dim sqlStr3 As String = "select * from tbl_order_detail"
comm = New OdbcCommand(sqlStr3, conn)
Dim myDA3 As OdbcDataAdapter = New OdbcDataAdapter(comm)
conn.Close()
myDA3.Fill(ds, "tbl_order_detail")
Dim orders = ds.Tables("tbl_order").AsEnumerable()
Dim details = ds.Tables("tbl_order_detail").AsEnumerable()
Dim query2 = _
From order In orders _
Group Join detail In details On order.Field(Of Integer)("or_id") _
Equals detail.Field(Of Integer)("or_id") Into ords = Group _
Select New With _
{ _
.CustomerID = order.Field(Of Integer)("or_id"), _
.ords = ords.Count(), _
.AverageListPrice = ords.Average(Function(detail) _
detail.Field(Of Integer)("or_id"))
}
For Each order In query2
Console.WriteLine("CustomerID: {0} Orders Count: {1} Orders avg: {2}", _
order.CustomerID, order.ords, order.AverageListPrice)
Next
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Form2.Show()
End Sub
End Class
No comments:
Post a Comment