Jump to content
San Roman

INNER JOIN com 3 tabelas e soma de campos

Recommended Posts

San Roman

Boa tarde.

Estou com um problema em um datagridview. 

Não dá erro, mas a soma dos campos está incorreta. Segue código:

Try
                Using strCom As FbCommand = New FbCommand("SELECT a.Conta, SUM(b.Valor), SUM(c.Valor) " & _
                                                          "FROM tbConta as a " & _
                                                          "INNER JOIN tbReceita as b ON b.Login = a.Login " & _
                                                          "INNER JOIN tbDespesa as c ON c.Login = a.Login " & _
                                                          "WHERE EXTRACT(MONTH FROM b.DataMovimento) = @mes " & _
                                                          "AND EXTRACT(YEAR FROM b.DataMovimento) = @ano " & _
                                                          "AND a.Titular = @titular " & _
                                                          "AND a.Login = @login " & _
                                                          "GROUP BY a.Conta", con)

                    strCom.Parameters.AddWithValue("@mes", frmPrincipal.txtMesRef.Text)
                    strCom.Parameters.AddWithValue("@ano", frmPrincipal.txtAnoRef.Text)
                    strCom.Parameters.AddWithValue("@titular", frmLogin.txtNome.Text)
                    strCom.Parameters.AddWithValue("@login", frmLogin.cbNome.Text)

                    Using da As FbDataAdapter = New FbDataAdapter(strCom)

                        Dim dt As DataTable = New DataTable("Receita")
                        Dim ds As DataSet = New DataSet

                        dgvTotalConta.Columns.Clear()
                        da.Fill(ds)

                        With dgvTotalConta

                            .DefaultCellStyle.Font = New Font("Arial", 8, FontStyle.Regular)
                            .ColumnHeadersDefaultCellStyle.Font = New Font("Arial", 8, FontStyle.Regular)
                            .GridColor = Color.LightGray
                            .BorderStyle = BorderStyle.Fixed3D
                            .RowHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single
                            .ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single
                            .RowHeadersVisible = False
                            .BackgroundColor = Color.White
                            .ColumnCount = 4
                            .AutoGenerateColumns = False
                            .Columns(0).Name = "Conta"
                            .Columns(1).Name = "Receita"
                            .Columns(2).Name = "Despesa"
                            .Columns(0).Width = 120
                            .Columns(1).Width = 100
                            .Columns(2).Width = 100
                            .Columns(1).DefaultCellStyle.Format = "N"
                            .Columns(2).DefaultCellStyle.Format = "N"
                            .Columns(0).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft
                            .Columns(1).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
                            .Columns(2).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
                            .AllowUserToAddRows = False
                            .AllowUserToDeleteRows = False
                            .AllowUserToOrderColumns = False
                            .AllowUserToResizeColumns = False
                            .AllowUserToResizeRows = False

                        End With

                        dt = ds.Tables(0)

                        For Each linha As DataRow In dt.Rows
                            dgvTotalConta.Rows.Add(linha(0), linha(1), linha(2))
                        Next

                    End Using
                End Using

            Catch ex As Exception
                MsgBox(ex.ToString)
            End Try

Espero que possam me ajudar nessa questão da soma de valores.

Share this post


Link to post
Share on other sites
M6

Tens de ser mais claro e concreto na tua dúvida.

Dizeres que as contas não dão certo indica que partes do principio de que o pessoal do fórum sabe a que contas te referes.
Uma dica: antes de colocares a query no meio do código, escreve-a na base de dados para perceberes que a mesma está bem (sintática e semanticamente).


10 REM Generation 48K!
20 INPUT "URL:", A$
30 IF A$(1 TO 4) = "HTTP" THEN PRINT "400 Bad Request": GOTO 50
40 PRINT "404 Not Found"
50 PRINT "./M6 @ Portugal a Programar."

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.