Club Delphi  
    FTP   CCD     Buscar   Trucos   Trabajo   Foros

Retroceder   Foros Club Delphi > Principal > Varios
Registrarse FAQ Miembros Calendario Guía de estilo Temas de Hoy

Grupo de Teaming del ClubDelphi

Respuesta
 
Herramientas Buscar en Tema Desplegado
  #1  
Antiguo 06-05-2011
Rofocale Rofocale is offline
Miembro
 
Registrado: mar 2010
Posts: 182
Poder: 15
Rofocale Va por buen camino
sumar los campos de un stringgrid

Código Delphi [-]
 dmDatos.cdsCliente.Active := false;
        with dmDatos.qryListados do begin
            Close;
            SQL.Clear;
            SQL.Add('SELECT caja, numero, fecha, hora, estatus, total-iva AS subtotal,');
            SQL.Add('iva, total, cliente, clave FROM ventas v WHERE 1 = 1');

            if(chkCliente.Checked) then
                SQL.Add('AND cliente IN (SELECT clave FROM clientes WHERE nombre LIKE ''%' + txtClienteBusq.Text + '%'')');

            if(chkFecha.Checked) then begin
                SQL.Add('AND fecha >= ' + quotedstr(datetostr(FechaBusq.date)));
                //SQL.Add('AND fecha <= ''' + txtMesFin.Text + '/' + txtDiaFin.Text + '/' + txtAnioFin.Text + '''');
            end;
            Open;
        end;
        with dmDatos.cdsCliente do begin
            Active := true;

            txtRegistros.text := inttostr(dmDatos.cdsCliente.RecordCount);

            FieldByName('caja').DisplayLabel := 'Caja';
            FieldByName('caja').DisplayWidth := 4;
            FieldByName('numero').DisplayLabel := 'Remisión';
            FieldByName('numero').DisplayWidth := 8;
            FieldByName('fecha').DisplayLabel := 'Fecha';
            FieldByName('fecha').DisplayWidth := 9;
            FieldByName('hora').DisplayLabel := 'Hora';
            FieldByName('hora').DisplayWidth := 11;
            FieldByName('estatus').DisplayLabel := 'Estatus';
            FieldByName('estatus').DisplayWidth := 7;


buenas ese es el codigo que utilizo para buscar por fecha o por nombre ciertos campos en una base de datos y las muestro en un string grid
bueno lo que deseo saber es

Código Delphi [-]
  if(chkFecha.Checked) then begin
                SQL.Add('AND fecha >= ' + quotedstr(datetostr(FechaBusq.date)));

en esta parte quiero aumentarle codigo que realmente no se como podria hacer para en el string grid mostro una lista de 10 por ejemplo que se encontro segun la fecha 06/05/2011 quiero que esos 10 un campo el total se sume y me de todo el total de los 10
si el total de cada uno era 5 al sumarlo todo que salga 50 en un Edit
alguien me puede ayudar ?
gracias
Responder Con Cita
  #2  
Antiguo 06-05-2011
Avatar de Caral
[Caral] Caral is offline
Miembro Premium
 
Registrado: ago 2006
Posts: 7.659
Poder: 25
Caral Va por buen camino
Hola
No veo por ningun lado como insertas esas lineas en el stringrid. ?.
Saludos
__________________
Siempre Novato
Responder Con Cita
  #3  
Antiguo 06-05-2011
Avatar de gatosoft
[gatosoft] gatosoft is offline
Miembro Premium
 
Registrado: may 2003
Ubicación: Bogotá, Colombia
Posts: 833
Poder: 22
gatosoft Va camino a la fama
De acuerdo con el comentario de Caral... no se ve por donde utilizas el StringGrid... pienso que tienes confusión con el DBGrid, que es donde se muestra el contenido del dataset (ClientDataset en tu caso).

Pues bien, tienes dos opciones:

1) Realizar las sumas directamente en el SQL, antes de mostrar la información
2) recorrer el dataset y hacer la suma por la "via dolorosa", asi:

Código Delphi [-]
Var
dblTotalSuma : Double;
...

dblTotalSuma :=0;
cdsClientes.First;
While not cdsClientes.Eof do
Begin
  dblTotalSuma := dblTotalSuma + cdsClientes.FieldByName('Total').AsFloat;
  cdsClientes.Next;
end;//While

Edit1.text := FloatToStr(dblTotalSuma );

Espero haberte ayudado.
Responder Con Cita
  #4  
Antiguo 06-05-2011
Rofocale Rofocale is offline
Miembro
 
Registrado: mar 2010
Posts: 182
Poder: 15
Rofocale Va por buen camino
Código Delphi [-]
procedure TfrmVentasConsulta.btnBuscarClick(Sender: TObject);
begin
    //if(VerificaDatos) then begin
        dmDatos.cdsCliente.Active := false;
        with dmDatos.qryListados do begin
            Close;
            SQL.Clear;
            SQL.Add('SELECT caja, numero, fecha, hora, estatus, total-iva AS subtotal,');
            SQL.Add('iva, total, cliente, clave FROM ventas v WHERE 1 = 1');

            if(chkCliente.Checked) then
                SQL.Add('AND cliente IN (SELECT clave FROM clientes WHERE nombre LIKE ''%' + txtClienteBusq.Text + '%'')');

            if(chkFecha.Checked) then begin
                SQL.Add('AND fecha >= ' + quotedstr(datetostr(FechaBusq.date)));
                //SQL.Add('AND fecha <= ''' + txtMesFin.Text + '/' + txtDiaFin.Text + '/' + txtAnioFin.Text + '''');
            end;
            Open;
        end;
        with dmDatos.cdsCliente do begin
            Active := true;

            txtRegistros.text := inttostr(dmDatos.cdsCliente.RecordCount);

            FieldByName('caja').DisplayLabel := 'Caja';
            FieldByName('caja').DisplayWidth := 4;
            FieldByName('numero').DisplayLabel := 'Remisión';
            FieldByName('numero').DisplayWidth := 8;
            FieldByName('fecha').DisplayLabel := 'Fecha';
            FieldByName('fecha').DisplayWidth := 9;
            FieldByName('hora').DisplayLabel := 'Hora';
            FieldByName('hora').DisplayWidth := 11;
            FieldByName('estatus').DisplayLabel := 'Estatus';
            FieldByName('estatus').DisplayWidth := 7;
            FieldByName('subtotal').DisplayLabel := 'Subtotal';
            FieldByName('subtotal').DisplayWidth := 10;
           // (FieldByName('subtotal') as TNumericField).DisplayFormat := '#,##0.00';
            FieldByName('iva').DisplayLabel := 'IGV';
            FieldByName('iva').DisplayWidth := 10;
           // (FieldByName('iva') as TNumericField).DisplayFormat := '#,##0.00';
            FieldByName('total').DisplayLabel := 'Total';
            FieldByName('total').DisplayWidth := 10;
           // (FieldByName('total') as TNumericField).DisplayFormat := '#,##0.00';
            FieldByName('cliente').Visible := false;
          //  FieldByName('usuario').Visible := false;
            FieldByName('clave').Visible := False;
        end;

        if(pgeGeneral.ActivePage = tabBusqueda) then
            grdListado.SetFocus;
    //end;
end;

ese es todo el codigo

y en el stringgrid enlazo en el datasource el correspondiente datasource
Responder Con Cita
  #5  
Antiguo 06-05-2011
Avatar de oscarac
[oscarac] oscarac is offline
Miembro Premium
 
Registrado: sep 2006
Ubicación: Lima - Perú
Posts: 2.010
Poder: 20
oscarac Va por buen camino
Cita:
Empezado por Rofocale Ver Mensaje
en esta parte quiero aumentarle codigo que realmente no se como podria hacer para en el string grid mostro una lista de 10 por ejemplo que se encontro segun la fecha 06/05/2011 quiero que esos 10 un campo el total se sume y me de todo el total de los 10


si el total de cada uno era 5 al sumarlo todo que salga 50 en un Edit
alguien me puede ayudar ?
gracias
esto no lo entendi... que quieres sumar?

otra pregnuta
porque usar stringrid y no dbgrid?
__________________
Dulce Regalo que Satanas manda para mi.....
Responder Con Cita
  #6  
Antiguo 06-05-2011
Avatar de ecfisa
ecfisa ecfisa is offline
Moderador
 
Registrado: dic 2005
Ubicación: Tres Arroyos, Argentina
Posts: 10.508
Poder: 36
ecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to behold
Cita:
Empezado por Rofocale Ver Mensaje
y en el stringgrid enlazo en el datasource el correspondiente datasource
Hola.

Digo yo Rofocale... ¿ No será un TDBGrid ?

Saludos.
__________________
Daniel Didriksen

Guía de estilo - Uso de las etiquetas - La otra guía de estilo ....

Última edición por ecfisa fecha: 06-05-2011 a las 21:53:42.
Responder Con Cita
  #7  
Antiguo 06-05-2011
Rofocale Rofocale is offline
Miembro
 
Registrado: mar 2010
Posts: 182
Poder: 15
Rofocale Va por buen camino
disculpa caral si es un TDBGrid sabeis como puedo hacer para que me sume todos los campos y pasarlos a un edit como lo mencione ?
muchas gracias
Responder Con Cita
  #8  
Antiguo 06-05-2011
Avatar de oscarac
[oscarac] oscarac is offline
Miembro Premium
 
Registrado: sep 2006
Ubicación: Lima - Perú
Posts: 2.010
Poder: 20
oscarac Va por buen camino
Puedes hacer lo que menciono gatosoft


Cita:
Empezado por gatosoft Ver Mensaje
De acuerdo con el comentario de Caral... no se ve por donde utilizas el StringGrid... pienso que tienes confusión con el DBGrid, que es donde se muestra el contenido del dataset (ClientDataset en tu caso).

Pues bien, tienes dos opciones:

1) Realizar las sumas directamente en el SQL, antes de mostrar la información
2) recorrer el dataset y hacer la suma por la "via dolorosa", asi:


Código Delphi [-]Var
dblTotalSuma : Double;
...

dblTotalSuma :=0;
cdsClientes.First;
While not cdsClientes.Eof do
Begin
dblTotalSuma := dblTotalSuma + cdsClientes.FieldByName('Total').AsFloat;
cdsClientes.Next;
end;//While

Edit1.text := FloatToStr(dblTotalSuma );




Espero haberte ayudado.

pero aun no entiendo que cosa quieres sumar?
__________________
Dulce Regalo que Satanas manda para mi.....
Responder Con Cita
  #9  
Antiguo 06-05-2011
Avatar de Caral
[Caral] Caral is offline
Miembro Premium
 
Registrado: ago 2006
Posts: 7.659
Poder: 25
Caral Va por buen camino
Hola
Tienes varias cosas que se pueden sumar en tu consulta:
Código SQL [-]
SELECT caja, numero, fecha, hora, estatus, total-iva AS subtotal,
iva, total, cliente, clave FROM ventas v '
Segun entiendo tienes un cliente X y una fecha X.
Lo que quieres hacer es filtrar la tabla segun estos y sumar esos campos.
Es asi????.
Saludos
__________________
Siempre Novato
Responder Con Cita
  #10  
Antiguo 06-05-2011
Avatar de Caral
[Caral] Caral is offline
Miembro Premium
 
Registrado: ago 2006
Posts: 7.659
Poder: 25
Caral Va por buen camino
Hola
Código Delphi [-]
procedure TfrmVentasConsulta.btnBuscarClick(Sender: TObject);
begin
    //if(VerificaDatos) then begin
        dmDatos.cdsCliente.Active := false;
        with dmDatos.qryListados do begin
            Close;
            SQL.Clear;
            SQL.Add('SELECT caja, numero, fecha, hora, estatus, Sum((total)-Sum(iva)) AS subtotal, ');
            SQL.Add('Sum(iva) As iva, Sum(total) As Total, cliente, clave FROM ventas v WHERE 1 = 1 ');

            if(chkCliente.Checked) then
                SQL.Add('AND cliente IN (SELECT clave FROM clientes WHERE nombre LIKE ''%' + txtClienteBusq.Text + '%'') ');

            if(chkFecha.Checked) then begin
                SQL.Add('AND fecha >= ' + quotedstr(datetostr(FechaBusq.date)));
                //SQL.Add('AND fecha <= ''' + txtMesFin.Text + '/' + txtDiaFin.Text + '/' + txtAnioFin.Text + '''');
            end;
            Open;
        end;
Saludos
__________________
Siempre Novato
Responder Con Cita
  #11  
Antiguo 06-05-2011
Rofocale Rofocale is offline
Miembro
 
Registrado: mar 2010
Posts: 182
Poder: 15
Rofocale Va por buen camino
normalmente al apretar el boton buscar solamente me va a listar cosas del dia 4,3,2,5,6 si aprieto un check que tengo y pongo la fecha 6
Código Delphi [-]
if(chkFecha.Checked) then begin
                SQL.Add('AND fecha >= ' + quotedstr(datetostr(FechaBusq.date)));
                //SQL.Add('AND fecha <= ''' + txtMesFin.Text + '/' + txtDiaFin.Text + '/' + txtAnioFin.Text + '''');
            end;

entonces solamente me lista las cosas del dia 6 es ahi donde quiero que tambien en un edit me salga si en el dia 6 aparece 4 filas de las compras que se hizo tal dia.. del cual tiene un campo total cada uno quisiera que este campo total se sume si el total de cada fila es 5,4,2,1 entonces quisiera que en el edit aparesca 12

solamente quiero que aparesca el total en el edit al filtrarlo para un dia especifico
Responder Con Cita
  #12  
Antiguo 06-05-2011
Avatar de Caral
[Caral] Caral is offline
Miembro Premium
 
Registrado: ago 2006
Posts: 7.659
Poder: 25
Caral Va por buen camino
Hola
Sumar el total y mostrarlo en un edit.
Código Delphi [-]
procedure TfrmVentasConsulta.btnBuscarClick(Sender: TObject);
begin
    //if(VerificaDatos) then begin
        dmDatos.cdsCliente.Active := false;
        with dmDatos.qryListados do begin
            Close;
            SQL.Clear;
            SQL.Add('SELECT caja, numero, fecha, hora, estatus, total-iva AS subtotal, ');
            SQL.Add('iva, Total, Sum(total) As GTotal, cliente, clave FROM ventas v WHERE 1 = 1 ');

            if(chkCliente.Checked) then
                SQL.Add('AND cliente IN (SELECT clave FROM clientes WHERE nombre LIKE ''%' + txtClienteBusq.Text + '%'') ');

            if(chkFecha.Checked) then begin
                SQL.Add('AND fecha >= ' + quotedstr(datetostr(FechaBusq.date)));
                //SQL.Add('AND fecha <= ''' + txtMesFin.Text + '/' + txtDiaFin.Text + '/' + txtAnioFin.Text + '''');
            end;
            Open;
            // Muestro el dato en un edit.
            Edit1.Text:= dmDatos.cdsCliente.Filebyname('GTotal').Value;
        end;
Saludos
PD: Mas o menos
__________________
Siempre Novato
Responder Con Cita
  #13  
Antiguo 07-05-2011
Rofocale Rofocale is offline
Miembro
 
Registrado: mar 2010
Posts: 182
Poder: 15
Rofocale Va por buen camino
no puedo al agregar lo que me has puesto caral me sale error al apretar el boton buscar me dice : invalid expression in the select list(not contained in either an aggregate function or the GROUP by clause)
Responder Con Cita
  #14  
Antiguo 07-05-2011
Avatar de Caral
[Caral] Caral is offline
Miembro Premium
 
Registrado: ago 2006
Posts: 7.659
Poder: 25
Caral Va por buen camino
Hola
Hace falta el group by en la sentencia, eso es todo.
Saludos
Edito:
Al hacer dos select uno dentro del otro, se necesita definir un Group by, ya que no se define de que tabla se hara la suma.
Me parece una consulta sencilla, pero hecha a lo dificil.
__________________
Siempre Novato

Última edición por Caral fecha: 07-05-2011 a las 19:58:31.
Responder Con Cita
  #15  
Antiguo 07-05-2011
Rofocale Rofocale is offline
Miembro
 
Registrado: mar 2010
Posts: 182
Poder: 15
Rofocale Va por buen camino
no funciona bueno hize esto pero me suma todo osea al hacer click en el check y poner la fecha me lo filtra normal pero me suma todo sin distinguir que puse la fecha y solo sumar el resultado de acuerdo a la fecha

Nombre total Fecha
Jose 2.5 05/05/2011
Maria 3.5 05/05/2011
Carlos 1.5 06/05/2011

al filtrar para la fecha 5 sale solo jose y maria pero la suma sale 7.5 me suma todo cuando solo deberia de salir 6

Código Delphi [-]
     dmDatos.cdsCliente.Active := false;
        with dmDatos.qryListados do begin
            Close;
            SQL.Clear;
            SQL.Add('SELECT caja, numero, fecha, hora, estatus, total-iva AS subtotal,');
            SQL.Add('iva, total, cliente, clave FROM ventas v WHERE 1 = 1');

            if(chkCliente.Checked) then
                SQL.Add('AND cliente IN (SELECT clave FROM clientes WHERE nombre LIKE ''%' + txtClienteBusq.Text + '%'')');

            if(chkFecha.Checked) then begin
                SQL.Add('AND fecha = ' + quotedstr(datetostr(FechaBusq.date)));

                with dmDatos.qryConsulta do begin
                Close;
                SQL.Clear;
                SQL.Add('SELECT sum(total) AS total FROM ventas');
                Open;
                txtTotalventa.text := floattostr(FieldByName('total').AsFloat);
                end;
            end;
            open;
Responder Con Cita
  #16  
Antiguo 07-05-2011
Rofocale Rofocale is offline
Miembro
 
Registrado: mar 2010
Posts: 182
Poder: 15
Rofocale Va por buen camino
alguien me ayuda porfavor ?
Responder Con Cita
  #17  
Antiguo 08-05-2011
Avatar de Caral
[Caral] Caral is offline
Miembro Premium
 
Registrado: ago 2006
Posts: 7.659
Poder: 25
Caral Va por buen camino
Hola
Código Delphi [-]
// primera consulta
dmDatos.cdsCliente.Active := false;
  with dmDatos.qryListados do begin
            Close;
            Text:= 'SELECT caja, numero, fecha, hora, estatus, total-iva AS subtotal,'+
                   'iva, total, cliente, clave FROM ventas v WHERE 1 = 1 ';

            if(chkCliente.Checked) then
            Begin
            SQL.Add('AND cliente IN (SELECT clave FROM clientes WHERE nombre LIKE ''%' + txtClienteBusq.Text + '%'')');
            end;
            if(chkFecha.Checked) then
            Begin
            SQL.Add('AND fecha = ' + quotedstr(datetostr(FechaBusq.date)));
            end;
 open;
 end;
  
// segunda consulta
with dmDatos.qryConsulta do begin
            Close;
            Text:= 'SELECT sum(total) AS total FROM ventas '+
                   'AND fecha = ' + quotedstr(datetostr(FechaBusq.date));
  Open;
  end;
// muestra dato
txtTotalventa.text := floattostr(FieldByName('total').AsFloat);
Saludos
__________________
Siempre Novato

Última edición por Caral fecha: 08-05-2011 a las 01:16:20.
Responder Con Cita
  #18  
Antiguo 09-05-2011
Rofocale Rofocale is offline
Miembro
 
Registrado: mar 2010
Posts: 182
Poder: 15
Rofocale Va por buen camino
Hola al compilar da error : no se puede asignar a una propiedad de solo lectura
Text:= 'SELECT.......
Responder Con Cita
  #19  
Antiguo 09-05-2011
Avatar de Caral
[Caral] Caral is offline
Miembro Premium
 
Registrado: ago 2006
Posts: 7.659
Poder: 25
Caral Va por buen camino
Hola
El error lo da en la segunda consulta y es muy logico, donde esta el where ???.
Código Delphi [-]
  
// segunda consulta
with dmDatos.qryConsulta do begin
            Close;
            Text:= 'SELECT sum(total) AS total FROM ventas '+
                   'Where fecha = ' + quotedstr(datetostr(FechaBusq.date));
  Open;
  end;
// muestra dato
txtTotalventa.text := floattostr(FieldByName('total').AsFloat);
Eso te pasa como a mi, copiar y pegar sin fijarnos....
Saludos
__________________
Siempre Novato
Responder Con Cita
  #20  
Antiguo 09-05-2011
Rofocale Rofocale is offline
Miembro
 
Registrado: mar 2010
Posts: 182
Poder: 15
Rofocale Va por buen camino
no era de la consulta sale error del Text dice que no se puede asignar a una propiedad de solo lectura.. por cierto ya cambie y le puse where y sigue saliendo el mismo error cannot assign to a read-only property
Responder Con Cita
Respuesta



Normas de Publicación
no Puedes crear nuevos temas
no Puedes responder a temas
no Puedes adjuntar archivos
no Puedes editar tus mensajes

El código vB está habilitado
Las caritas están habilitado
Código [IMG] está habilitado
Código HTML está deshabilitado
Saltar a Foro

Temas Similares
Tema Autor Foro Respuestas Último mensaje
sumar campos cmm07 Varios 2 23-03-2009 16:21:08
Sumar valores con stringgrid zero_dx Varios 2 16-10-2007 20:02:01
sumar columnas en stringgrid Choclito Varios 1 11-05-2006 15:26:59
Sumar los campos de 3 tablas luisreg SQL 2 28-01-2004 02:34:41
Sumar campos de una consulta? danytorres Varios 1 10-11-2003 15:52:57


La franja horaria es GMT +2. Ahora son las 19:06:22.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi
Copyright 1996-2007 Club Delphi