【问题标题】:how to group by dataview in vb.net如何在 vb.net 中按数据视图分组
【发布时间】:2021-02-11 23:52:56
【问题描述】:

我想在数据视图上使用 group by 并应用过滤器。

    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

我想 GroupBy xProdID 和 xPrice 并且过滤器是 (xAccID = ? and (xDate = ??)) 并获得 xQuan 的总和和 xCarCount 的总和以进行分组。 我该如何应用?

【问题讨论】:

  • VB.NET 和这个有什么关系?请添加一些VB.NET代码
  • @CaiusJard 我又更新了
  • 看起来你只是缺少一个, grp.Sum(Function(row) row.Field(Of Double)("xCarCount"))
  • @CaiusJard 它给了我一个错误:“指定的演员表无效”
  • xCarCount 是整数吗? , grp.Sum(Function(row) row.Field(Of Integer)("xCarCount"))

标签: mysql vb.net datatable dataview


【解决方案1】:
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

【讨论】:

    猜你喜欢
    • 2021-12-20
    • 1970-01-01
    • 2023-04-06
    • 1970-01-01
    • 1970-01-01
    • 2010-11-12
    • 1970-01-01
    • 1970-01-01
    • 2019-09-08
    相关资源
    最近更新 更多