Fórum erro group by #130861

22/03/2010

0

Este é meu codigo para uma consulta, porem esta apresentando um erro no group by   

SQL = "SELECT " & _
                          "  A.ID_TALHOES, A.NOME,B.CULTURA, " & _
                          "  (Select Coalesce(Sum(RAM.VALORT_MAQUINARIOS),0) " & _
                          "   From REGISTRO_APLICACOES_MAQUINARIO RAM " & _
                          "   where RAM.COD_REGISTRO_APLICACOES=C.ID_REGISTRO_APLICACOES) as VALOR_TOTAL_MAQUINARIOS, " & _
                          "  (Select Coalesce(Sum(RAI.VALOR_TOTAL),0) " & _
                          "   From REGISTRO_APLICACOES_INSUMO RAI " & _
                          "   where RAI.COD_REGISTRO_APLICACOES=C.ID_REGISTRO_APLICACOES) as VALOR_TOTAL_INSUMOS, " & _
                          "  (Select Coalesce(Sum(RAF.VALORT_FUNCIONARIOS),0) " & _
                          "   From REGISTRO_APLICACOES_FUNC RAF " & _
                          "   where RAF.COD_REGISTROS_APLICACOES=C.ID_REGISTRO_APLICACOES) as VALOR_TOTAL_FUNCIONARIOS, " & _
                          "  (Select Coalesce(Sum(RAM.VALORT_MAQUINARIOS),0) " & _
                          "   From REGISTRO_APLICACOES_MAQUINARIO RAM " & _
                          "   where RAM.COD_REGISTRO_APLICACOES=C.ID_REGISTRO_APLICACOES) + " & _
                          "  (Select Coalesce(Sum(RAI.VALOR_TOTAL),0) " & _
                          "   From REGISTRO_APLICACOES_INSUMO RAI " & _
                          "   where RAI.COD_REGISTRO_APLICACOES=C.ID_REGISTRO_APLICACOES) + " & _
                          "  (Select Coalesce(Sum(RAF.VALORT_FUNCIONARIOS),0) " & _
                          "   From REGISTRO_APLICACOES_FUNC RAF " & _
                          "   where RAF.COD_REGISTROS_APLICACOES=C.ID_REGISTRO_APLICACOES) as VALOR_TOTAL " & _
                          "FROM CAD_TALHOES A " & _
                          "LEFT JOIN CAD_CULTURAS B ON B.ID_CULTURA = A.COD_CULTURA " & _
                          "LEFT JOIN REGISTRO_APLICACOES C ON C.COD_TALHAO = A.ID_TALHOES "

            If chkData.Value = 1 Then
                    If txtDataInicial.Text <> "__/__/____" And txtDataFinal.Text <> "__/__/____" Then
                       SQL = SQL & " AND C.DATA_INICIAL BETWEEN '" & Format(txtDataInicial.Text, "yyyy-mm-dd") & "' AND '" & Format(txtDataFinal.Text, "yyyy-mm-dd") & "' "
                    Else
                       MsgBox "Informe as datas", vbCritical, "Aviso..."
                    End If
            End If
            
            SQL = SQL & " WHERE C.COD_FAZENDA = " & Identificador_fazenda
           
           
            If chkCulturas.Value = 1 Then
               If txtCodCultura.Text <> "" Then
                  SQL = SQL & " AND B.ID_CULTURA = " & txtCodCultura.Text
               Else
                  MsgBox "Informe o tipo de maquinário", vbCritical, "Aviso..."
               End If
            End If
           
            If chkTalhao.Value = 1 Then
                If txtTalhaoInicial.Text <> "" And txtTalhaoFinal.Text <> "" Then
                    SQL = SQL & " AND C.COD_TALHAO>=" & txtTalhaoInicial.Text
                    SQL = SQL & " AND C.COD_TALHAO<=" & txtTalhaoFinal.Text
                Else
                    MsgBox "Informe Talhões", vbCritical, "Aviso..."
                End If
            End If
           
            If chkSafra.Value = 1 Then
                If txtSafra.Text <> "" Then
                    SQL = SQL & " AND C.SAFRA=" & txtSafra.Value
                Else
                    MsgBox "Informe a Safra", vbCritical, "Aviso..."
                End If
            End If
           
            SQL = SQL & " GROUP BY  A.ID_TALHOES, A.NOME, B.CULTURA "

Porem apresenta este erro.

sql error code=-104

invalid expression in the select list ( not contained in the eitheran aggregate function or the Group By claused)


Se tirar o Group By funciona, mas os dados não veem agrupados.            
Paulo Henrique

Paulo Henrique

Responder

Posts

23/03/2010

Emerson Nascimento

creio que C.ID_REGISTRO_APLICACOES deve ser incluído no group by



Responder

Gostei + 0

23/03/2010

Paulo Henrique

Era isto mesmo, porem não agrupou. Muito estranho isto. Ele teria que agrupar de acordo com os talhões.
Responder

Gostei + 0

23/03/2010

Emerson Nascimento

creio que esse caso seja melhor resolvido com stored procedures.
Responder

Gostei + 0

23/03/2010

Wilson Junior

Teste o seu agrupamento somente com um dos valores (Subselect), e verifique se os campos de igualdade (índices por exemplo) estão iguais aos das tabelas do FROM do SQL principal.

Espero ter colaborado.
Responder

Gostei + 0

Utilizamos cookies para fornecer uma melhor experiência para nossos usuários, consulte nossa política de privacidade.

Aceitar