从vb.net的另一个表中的SQL中更新集合总和

发布于 2025-01-29 16:34:39 字数 2020 浏览 4 评论 0原文

我想更新设定总和,但是我的代码并不完美,也许我需要添加或修改SQL。 我尝试了“ UpdateTable2()”,但结果尚未符合我想要的输出结果 谢谢

TABLE1          
INVNO   QTY PRICE   DIS
1000    10  10000   10
1000    20  20000   20
1001    15  10000   10
1001    30  20000   20


TABLE2  
INVNO    TOTAL
1000    
1001    

OUTPUT
TABLE2  
INVNO   TOTAL
1000    410000   QTY*PRICE*(1-DIS/100) for total from INVNO 1000
1001    615000   QTY*PRICE*(1-DIS/100) for total from INVNO 1001
  Private Sub fillDataGrid()
        Try
            Dim query As String = "SELECT INVNO,SUM((QTY*PRICE)*(1-DIS/100)) AS TOTAL FROM TABLE1 GROUP BY INVNO"
            Using con As OleDbConnection = New OleDbConnection(cn)
                Using cmd As OleDbCommand = New OleDbCommand(query, con)
                    'cmd.Parameters.AddWithValue("@CODE", ComboBox1.SelectedValue)
                    Using da As New OleDbDataAdapter(cmd)
                        Dim dt As DataTable = New DataTable()
                        da.Fill(dt)
                        da.Dispose()
                        source3.DataSource = dt
                        Me.DataGridView3.DataSource = source3
                        Me.DataGridView3.Refresh()
                    End Using
                End Using
            End Using
        Catch ex As Exception
        End Try
    End Sub
Sub UpdateTABLE2()
        Try
 Dim sql As String = "UPDATE TABLE2 INNER JOIN TABLE1 ON TABLE1.[INVNO] = TABLE2.[INVNO] SET TABLE2.[TOTAL] = [QTY]*[PRICE]*(1-[DIS]/100)"
Using conn As New OleDbConnection(cn),
 cmd As New OleDbCommand(sql, conn)
 'cmd.Parameters.AddWithValue("@INVNO", ComboBox1.SelectedValue)
 conn.Open()
 cmd.ExecuteNonQuery()
 End Using

I want to update the set sum but my code is not perfect, maybe I need to add or modify the sql.
I tried "UpdateTABLE2()" but the result is not yet in accordance with the output result I want
thanks

TABLE1          
INVNO   QTY PRICE   DIS
1000    10  10000   10
1000    20  20000   20
1001    15  10000   10
1001    30  20000   20


TABLE2  
INVNO    TOTAL
1000    
1001    

OUTPUT
TABLE2  
INVNO   TOTAL
1000    410000   QTY*PRICE*(1-DIS/100) for total from INVNO 1000
1001    615000   QTY*PRICE*(1-DIS/100) for total from INVNO 1001
  Private Sub fillDataGrid()
        Try
            Dim query As String = "SELECT INVNO,SUM((QTY*PRICE)*(1-DIS/100)) AS TOTAL FROM TABLE1 GROUP BY INVNO"
            Using con As OleDbConnection = New OleDbConnection(cn)
                Using cmd As OleDbCommand = New OleDbCommand(query, con)
                    'cmd.Parameters.AddWithValue("@CODE", ComboBox1.SelectedValue)
                    Using da As New OleDbDataAdapter(cmd)
                        Dim dt As DataTable = New DataTable()
                        da.Fill(dt)
                        da.Dispose()
                        source3.DataSource = dt
                        Me.DataGridView3.DataSource = source3
                        Me.DataGridView3.Refresh()
                    End Using
                End Using
            End Using
        Catch ex As Exception
        End Try
    End Sub
Sub UpdateTABLE2()
        Try
 Dim sql As String = "UPDATE TABLE2 INNER JOIN TABLE1 ON TABLE1.[INVNO] = TABLE2.[INVNO] SET TABLE2.[TOTAL] = [QTY]*[PRICE]*(1-[DIS]/100)"
Using conn As New OleDbConnection(cn),
 cmd As New OleDbCommand(sql, conn)
 'cmd.Parameters.AddWithValue("@INVNO", ComboBox1.SelectedValue)
 conn.Open()
 cmd.ExecuteNonQuery()
 End Using

enter image description here
RESULT

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

快乐很简单 2025-02-05 16:34:39

有3个问题要解决:

  1. 计算表1中每一行的总数。您可以将计算出的字段直接添加到表格,创建一个为您完成的视图,也可以使用表达式使用Inline 进行。我提出了一个视图:

     创建视图V_Table1
    作为
    选择 *,(cast(qty as Money) * cast(价格为货币) *(铸(1个货币) - (cast(dis as as Charate) / cast(100 as Chone))),如表_1的总计;
     

  2. 将数据分组。同样,可能只是查询或视图,在这里再次作为视图:

     创建视图v_table1_consolidated
     作为
     从v_table1中选择否,总和(总计)作为总数
     小组否
     
  3. 将其填充到表中。首先要挑战自己是否需要序列化,如果这是必需的,则可以使用以下代码之类的内容:(当心:它会暂时删除table2>)

     截断table2;
     插入table2中的select *从v_table1_consolidated中插入 *;
     

如果您有其他数据您已经省略了订单号,客户或时间范围,并且仅应为该订单/客户/时间范围刷新更新从table2中删除... 。

There are 3 problems to solve:

  1. Calculate the total for each row in TABLE1. You could add a calculated field directly to the table, create a view that does it for you or do it with an inline WITH expression. I propose a view:

    CREATE VIEW V_TABLE1
    AS
    SELECT *, (CAST(QTY AS MONEY) * CAST(Price AS MONEY) * (CAST(1 AS MONEY) - (CAST(DIS AS MONEY) / CAST(100 AS MONEY)))) AS Total FROM Table_1;
    
  2. Group the data. Again, could be just a query or a view, here again as a view:

     CREATE VIEW V_TABLE1_Consolidated
     AS
     SELECT NO, SUM(Total) AS Total FROM V_TABLE1
     GROUP BY NO
    
  3. Fill that into a table. Challenge yourself first whether it really needs to be serialized, and if that is a requirement, you could use something like the following code: (Watch out: It temporarily drops everything of TABLE2)

     TRUNCATE TABLE TABLE2;
     INSERT INTO TABLE2 SELECT * FROM V_TABLE1_Consolidated;
    

If you have additional data that you have omitted like an order number or a customer or a time range and the updates should only be refreshed for that order/customer/time range then you have to replace the TRUNCATE TABLE by a DELETE FROM TABLE2 WHERE ....

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文