如何在 vb.net 中按数据视图分组
how to group by dataview in vb.net
我想在数据视图上使用分组依据并应用过滤器。
SELECT xAccID,xAccName,xProdID,xProdName,
xPrice,
SUM(xQuan) AS xxQuan,xDate,SUM(xCarCount) AS xxCarCount FROM
(
(SELECT
`FatoraDate` AS xDate,
`FatoraReso` AS xAccID,
`AccName` AS xAccName,
`FatoraProduct` AS xProdID,
CONCAT(`CategoryName`,' ',`ProductName`) AS xProdName,
`FatoraPurPrice` AS xPrice,
SUM(`FatoraQuan`) AS xQuan,
COUNT(`FatoraReso`) AS xCarCount
FROM tblfatora
INNER JOIN tblaccounts
ON tblaccounts.AccID = tblfatora.FatoraReso
INNER JOIN tblproducts
ON tblproducts.ProductID = tblfatora.FatoraProduct
INNER JOIN tblcategories
ON tblcategories.CategoryID = tblproducts.ProductCategory
GROUP BY xAccID,xAccName,xDate,xProdID,xPrice
ORDER BY xDate,xAccID,xProdID)
UNION ALL
(SELECT
`FatoraDate` AS xDate,
`FatoraCustomer` AS xAccID,
`AccName` AS xAccName,
`FatoraProduct` AS xProdID,
CONCAT(`CategoryName`,' ',`ProductName`) AS xProdName,
`FatoraSalePrice` AS xPrice,
SUM(`FatoraQuan`) AS xQuan,
COUNT(`FatoraCustomer`) AS xCarCount
FROM tblfatora
INNER JOIN tblaccounts
ON tblaccounts.AccID = tblfatora.FatoraCustomer
INNER JOIN tblproducts
ON tblproducts.ProductID = tblfatora.FatoraProduct
INNER JOIN tblcategories
ON tblcategories.CategoryID = tblproducts.ProductCategory
GROUP BY xAccID,xAccName,xDate,xProdID,xPrice
ORDER BY xDate,xAccID,xProdID
)) tbl1
GROUP BY xDate,xAccID,xAccName,xProdID,xProdName,xPrice
ORDER BY `tbl1`.`xAccName` ASC
我在加载时填写 MyVar_Dt_Quan 并预先准备过滤器:
MyPubVar_Sort = "xProdID,xPrice"
MyPubVar_Filter = ""
MyPubVar_Filter = " xAccID = " & xAccID & "
AND
(xDate >= '" & xDate1 & "' AND xDate <= '" & xDate2 & "') "
MyVar_Dv_Quan = MyVar_Dt_Quan.DefaultView
MyVar_Dv_Quan.RowFilter = MyPubVar_Filter
Dim fruitGroups = MyVar_Dv_Quan.ToTable.AsEnumerable().
GroupBy(Function(row) New With {
Key .xProdID = row.Field(Of Double)("xProdID"),
Key .xPrice = row.Field(Of Double)("xPrice")
})
Dim tableResult = MyVar_Dv_Quan.ToTable.Clone()
For Each grp In fruitGroups
tableResult.Rows.Add(grp.Key.xProdID,
grp.Key.xPrice,
grp.Sum(Function(row) row.Field(Of Double)("xxQuan")))
Next
Me.Dgv2.DataSource = tableResult
我想对 xProdID 和 xPrice 进行分组,过滤器是 (xAccID = ? and (xDate <= ?? and >= ??)) 并获取 xQuan 的总和和 xCarCount 的总和作为分组依据。
我该如何应用它?
MyPubVar_Sort = "xProdID,xPrice"
MyPubVar_Filter = ""
MyPubVar_Filter = " xAccID = " & xAccID & "
AND
(xDate >= '" & xDate1 & "' AND xDate <= '" & xDate2 & "') "
MyVar_Dv_Quan = MyVar_Dt_Quan.DefaultView
MyVar_Dv_Quan.RowFilter = MyPubVar_Filter
Dim fruitGroups = MyVar_Dv_Quan.ToTable.AsEnumerable().
GroupBy(Function(row) New With {
Key .xAccID = row.Field(Of Int32)("xAccID"),
Key .xAccName = row.Field(Of String)("xAccName"),
Key .xProdID = row.Field(Of Int32)("xProdID"),
Key .xProdName = row.Field(Of String)("xProdName"),
Key .xPrice = row.Field(Of Double)("xPrice")
})
Dim tableResult = MyVar_Dv_Quan.ToTable.Clone()
For Each grp In fruitGroups
tableResult.Rows.Add(
grp.Key.xAccID,
grp.Key.xAccName,
grp.Key.xProdID,
grp.Key.xProdName,
grp.Key.xPrice,
grp.Sum(Function(row) row.Field(Of Double)("xxQuan")),
grp.Max(Function(row) row.Field(Of Date)("xDate")),
grp.Sum(Function(row) row.Field(Of Object)("xxCarCount")))
Next
Me.Dgv2.DataSource = tableResult
我想在数据视图上使用分组依据并应用过滤器。
SELECT xAccID,xAccName,xProdID,xProdName,
xPrice,
SUM(xQuan) AS xxQuan,xDate,SUM(xCarCount) AS xxCarCount FROM
(
(SELECT
`FatoraDate` AS xDate,
`FatoraReso` AS xAccID,
`AccName` AS xAccName,
`FatoraProduct` AS xProdID,
CONCAT(`CategoryName`,' ',`ProductName`) AS xProdName,
`FatoraPurPrice` AS xPrice,
SUM(`FatoraQuan`) AS xQuan,
COUNT(`FatoraReso`) AS xCarCount
FROM tblfatora
INNER JOIN tblaccounts
ON tblaccounts.AccID = tblfatora.FatoraReso
INNER JOIN tblproducts
ON tblproducts.ProductID = tblfatora.FatoraProduct
INNER JOIN tblcategories
ON tblcategories.CategoryID = tblproducts.ProductCategory
GROUP BY xAccID,xAccName,xDate,xProdID,xPrice
ORDER BY xDate,xAccID,xProdID)
UNION ALL
(SELECT
`FatoraDate` AS xDate,
`FatoraCustomer` AS xAccID,
`AccName` AS xAccName,
`FatoraProduct` AS xProdID,
CONCAT(`CategoryName`,' ',`ProductName`) AS xProdName,
`FatoraSalePrice` AS xPrice,
SUM(`FatoraQuan`) AS xQuan,
COUNT(`FatoraCustomer`) AS xCarCount
FROM tblfatora
INNER JOIN tblaccounts
ON tblaccounts.AccID = tblfatora.FatoraCustomer
INNER JOIN tblproducts
ON tblproducts.ProductID = tblfatora.FatoraProduct
INNER JOIN tblcategories
ON tblcategories.CategoryID = tblproducts.ProductCategory
GROUP BY xAccID,xAccName,xDate,xProdID,xPrice
ORDER BY xDate,xAccID,xProdID
)) tbl1
GROUP BY xDate,xAccID,xAccName,xProdID,xProdName,xPrice
ORDER BY `tbl1`.`xAccName` ASC
我在加载时填写 MyVar_Dt_Quan 并预先准备过滤器:
MyPubVar_Sort = "xProdID,xPrice"
MyPubVar_Filter = ""
MyPubVar_Filter = " xAccID = " & xAccID & "
AND
(xDate >= '" & xDate1 & "' AND xDate <= '" & xDate2 & "') "
MyVar_Dv_Quan = MyVar_Dt_Quan.DefaultView
MyVar_Dv_Quan.RowFilter = MyPubVar_Filter
Dim fruitGroups = MyVar_Dv_Quan.ToTable.AsEnumerable().
GroupBy(Function(row) New With {
Key .xProdID = row.Field(Of Double)("xProdID"),
Key .xPrice = row.Field(Of Double)("xPrice")
})
Dim tableResult = MyVar_Dv_Quan.ToTable.Clone()
For Each grp In fruitGroups
tableResult.Rows.Add(grp.Key.xProdID,
grp.Key.xPrice,
grp.Sum(Function(row) row.Field(Of Double)("xxQuan")))
Next
Me.Dgv2.DataSource = tableResult
我想对 xProdID 和 xPrice 进行分组,过滤器是 (xAccID = ? and (xDate <= ?? and >= ??)) 并获取 xQuan 的总和和 xCarCount 的总和作为分组依据。 我该如何应用它?
MyPubVar_Sort = "xProdID,xPrice"
MyPubVar_Filter = ""
MyPubVar_Filter = " xAccID = " & xAccID & "
AND
(xDate >= '" & xDate1 & "' AND xDate <= '" & xDate2 & "') "
MyVar_Dv_Quan = MyVar_Dt_Quan.DefaultView
MyVar_Dv_Quan.RowFilter = MyPubVar_Filter
Dim fruitGroups = MyVar_Dv_Quan.ToTable.AsEnumerable().
GroupBy(Function(row) New With {
Key .xAccID = row.Field(Of Int32)("xAccID"),
Key .xAccName = row.Field(Of String)("xAccName"),
Key .xProdID = row.Field(Of Int32)("xProdID"),
Key .xProdName = row.Field(Of String)("xProdName"),
Key .xPrice = row.Field(Of Double)("xPrice")
})
Dim tableResult = MyVar_Dv_Quan.ToTable.Clone()
For Each grp In fruitGroups
tableResult.Rows.Add(
grp.Key.xAccID,
grp.Key.xAccName,
grp.Key.xProdID,
grp.Key.xProdName,
grp.Key.xPrice,
grp.Sum(Function(row) row.Field(Of Double)("xxQuan")),
grp.Max(Function(row) row.Field(Of Date)("xDate")),
grp.Sum(Function(row) row.Field(Of Object)("xxCarCount")))
Next
Me.Dgv2.DataSource = tableResult