简体   繁体   中英

UPDATE SET SUM IN SQL FROM ANOTHER TABLE IN VB.NET

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

在此处输入图像描述 结果

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... .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM