Club Delphi  
    FTP   CCD     Buscar   Trucos   Trabajo   Foros

Retroceder   Foros Club Delphi > Principal > SQL
Registrarse FAQ Miembros Calendario Guía de estilo Buscar Temas de Hoy Marcar Foros Como Leídos

 
 
Herramientas Buscar en Tema Desplegado
  #6  
Antiguo 06-02-2018
Avatar de mRoman
mRoman mRoman is offline
Miembro
 
Registrado: nov 2003
Posts: 599
Poder: 21
mRoman Va por buen camino
Smile

Aqui el código desde delphi que utilizo para construir el SELECT
Código Delphi [-]
procedure TfrmOpDespacho.GroupBox4Exit(Sender: TObject);
var
  clave,i, i_difer :integer;
  wMes,wDia,wAnio:Word;
  cMes:String;
  Porcentaje:Real;
begin
   qryPlanta.Close;
   qryPlanta.Open;
   qryPlanta.FetchAll;
   qryPlanta.First;
   qryMesEmbarque.Close;
   qryMesEmbarque.Open;

   case cbxMes.ItemIndex+1 of
        1 :cMes:='a.Ene';
        2 :cMes:='a.Feb';
        3 :cMes:='a.Mar';
        4 :cMes:='a.Abr';
        5 :cMes:='a.Myo';
        6 :cMes:='a.Jun';
        7 :cMes:='a.Jul';
        8 :cMes:='a.Ago';
        9 :cMes:='a.Sep';
        10:cMes:='a.Oct';
        11:cMes:='a.Nov';
        12:cMes:='a.Dic';
   end;
   if not(cMes=' ') then
   begin
        
       IBQry.Close;
       IBQry.SQL.Clear;
       IBQry.SQL.Add('select a.anio,');
       IBQry.SQL.Add('       '+cMes+' as lts_prog_anual,');
       IBQry.SQL.Add('       (select sum(b.litros)');
       IBQry.SQL.Add('        from reporte_despacho1 b');
       IBQry.SQL.Add('        where extract(year from b.fecha_embarque)=:anio');
       IBQry.SQL.Add('          and extract(month from b.fecha_embarque)=:mes');
       IBQry.SQL.Add('          and b.producto like '''+'AS%'+''') as lts_despacho,');
       IBQry.SQL.Add('       ((select sum(c.litros)');
       IBQry.SQL.Add('           from reporte_despacho1 c');
       IBQry.SQL.Add('          where extract(year from c.fecha_embarque)=:anio');
       IBQry.SQL.Add('            and extract(month from c.fecha_embarque)=:mes');
       IBQry.SQL.Add('            and c.producto like '''+'AS%'+''' )-'+cMes+') as difer,');
       IBQry.SQL.Add('       ((select sum(d.litros)');
       IBQry.SQL.Add('           from reporte_despacho1 d');
       IBQry.SQL.Add('          where extract(year from d.fecha_embarque)=:anio');
       IBQry.SQL.Add('            and extract(month from d.fecha_embarque)=:mes');
       IBQry.SQL.Add('            and d.producto like '''+'AS%'+''' )/'+cMes+')*100 as porcentaje');
       IBQry.SQL.Add('from PROGRAMA_DISTRIBUCION_ANUAL A');
       IBQry.SQL.Add('where a.clave_clasificacion=3');
       IBQry.SQL.Add('  and a.clave_rubro=1');
       IBQry.SQL.Add('  and a.anio=:anio');
       IBQry.ParamByName('MES').AsInteger:=cbxMes.ItemIndex+1;
       IBQry.ParamByName('anio').AsString:=mskAnio.Text;
       IBQry.Open;

// Estos son los QUERYS que tuve q crear (2 componentes IBQuery) que hacen lo mismo que el SELECT q construyo mas arriba, solo que aca por
// separado.
       qryProgAnual.Close;
       qryProgAnual.ParamByName('anio').AsString:=mskAnio.Text;
       qryProgAnual.Open;

       qryLtsDespacho.Close;
       qryLtsDespacho.ParamByName('anio').AsString:=mskAnio.Text;
       qryLtsDespacho.ParamByName('mes').AsInteger:=cbxMes.ItemIndex+1;
       qryLtsDespacho.Open;
// Hasta aqui terminan los 2 querys

//La siguiente linea, realiza una operación aritmética para encontrar la DIFERENCIA de ambos datos.
       i_difer:=qryLtsDespacho.fieldByName('lts_despacho').AsInteger-qryProgAnual.fieldBYName('lts_prog_anual').AsInteger;

       mskDifer.Text:=IntToStr(i_difer);
//       if qryDifer.FieldByName('DIFER').AsInteger<0 then
       if i_difer<0 then
       begin
           Label3.Caption:='ABAJO CON:';
           img1.Picture:=imgAbajo.Picture;
       end Else
       begin
           Label3.Caption:='ARRIBA CON:';
           img1.Picture:=imgArriba.Picture;
       end;
       Porcentaje:=(qryLtsDespacho.fieldByName('lts_despacho').AsFloat/
                    qryProgAnual.fieldByName('lts_prog_anual').AsFloat)*100.00;
//       Label4.Caption:=qryElaboro.fieldbyname('difer').AsString+' Litros';
//       mskPorcentaje.Text:=FloatToStr(qryDifer.fieldbyname('porcentaje').AsFloat);
//       mskPorcentaje.Text:=FloatToStr(Porcentaje);

       mskPorcentaje.Text:=Format('%n',[Porcentaje]);
       DecodeDate(Date,wAnio,wMes,wDia);
   end Else
   begin
       // Todavia no agrego código aqui.
   end;
end;
__________________
Miguel Román

Afectuoso saludo desde tierras mexicanas....un aguachile?, con unas "cetaseas" bien "muertas"?, VENTE PUES !!
Responder Con Cita
 


Herramientas Buscar en Tema
Buscar en Tema:

Búsqueda Avanzada
Desplegado

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
Incorrect values within SQLDA structure fedelinardi SQL 5 01-02-2018 04:52:07
TMemoField y error SQLDA brandolin Firebird e Interbase 0 16-02-2009 19:55:03
ods (on disk structure) arrayman Firebird e Interbase 1 26-11-2007 00:30:48
insert into tabla values (select *... ¿? jorgesl SQL 15 11-04-2006 22:29:08
Problemas con un Grid ( integer values ) aragorneuf Varios 6 28-04-2004 01:18:20


La franja horaria es GMT +2. Ahora son las 21:21:26.


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