[firebase-br] RES: RES: Ajuda em SQL

Zottis bzottis em ig.com.br
Qua Mar 9 08:59:23 -03 2011


Eu uso um Subselect direto na listagem de clientes e no "RXDBGRID", no
evento GetCellParams eu faço as pesquisas.

Da uma olhada no código abaixo.

Ficará lento se retornar muitos registros, mas também não demora muito.


Espero que ajude.


Zottis

procedure TBuscaCtaRec.GradeContasGetCellParams(Sender: TObject; Field:
TField;
  AFont: TFont; var Background: TColor; Highlight: Boolean);
    var Pago_, Boleto_, SERASA_: variant;
begin
   Pago_     := (Sender as
TrxDBGrid).DataSource.DataSet.FieldByName('Pago').AsString;
   Boleto_   := (Sender as
TrxDBGrid).DataSource.DataSet.FieldByName('BOLETO_EMITIDO').AsString;
   SERASA_   := (Sender as
TrxDBGrid).DataSource.DataSet.FieldByName('SERASA').AsString;

  if  (Pago_ = 'S') then
    Background := clMoneyGreen
  else if Pago_ <>'S' then
    Begin
     AFont.Color := clRed;
     //Background := clInfoBk;
    end;
  if Field.FieldName = 'COD_CONTA' then
    begin
        Background  := cl3DLight;
        AFont.Style := AFont.Style + [fsBold];
        AFont.Color := clBlack;
         if (Boleto_ = 'S') then
            Begin
            Background := clSkyBlue;
            AFont.Color := clBlue;
            end else
          if Boleto_ <>'S' then
            Begin
             AFont.Color := clGreen;
             Background := clInfoBk;
            end;
    end;

  if Highlight then
   begin
    AFont.Color := clHighlightText;
    Background := clHighlight;
   end;
     if Field.FieldName = 'SITUACAO' then
    begin
        Background  := cl3DLight;
        AFont.Style := AFont.Style + [fsBold];
        AFont.Color := clBlack;
           IF Field.Value = 'Conta Aberta' THEN
             BEGIN
              Background  := clInfoBk;
              AFont.Style := AFont.Style + [fsBold];
             END ELSE
           IF Field.Value = 'Conta Vencida' THEN
             BEGIN
              Background  := ClRed;
              AFont.Style := AFont.Style + [fsBold];
              AFont.Color := clBlack;
             END ELSE
           IF Field.Value = 'Conta Paga' THEN
             BEGIN
              Background  := clMenu;
              AFont.Style := AFont.Style + [fsBold];
             END;
    end else
  if Field.FieldName = 'VENCIMENTO' then
    begin
      Background  := cl3DLight;
      AFont.Style := AFont.Style + [fsBold];
      AFont.Color := clBlack;
    end;
  if Field.FieldName = 'VALOR' then
    begin
      AFont.Style := AFont.Style + [fsBold];
    end;
  if Field.FieldName = 'COD_CONTA' then
    begin
      AFont.Style := AFont.Style + [fsBold];
    end;
  if Field.FieldName = 'SALDO' then
    begin
      AFont.Style := AFont.Style + [fsBold];
    end;

 if Field.FieldName = 'FORMA_PGTO' then
    begin
        Background  := cl3DLight;
        AFont.Style := AFont.Style + [fsBold];
        AFont.Color := clBlack;
           IF Field.Value = 'MENSAL' THEN
             BEGIN
              Background  := clRed;
              AFont.Style := AFont.Style + [fsBold];
             END ELSE
           IF Field.Value = 'MENSAL + EXCEDENTE' THEN
             BEGIN
              Background  := clSkyBlue;
              AFont.Style := AFont.Style + [fsBold];
              AFont.Color := clBlack;
             END ELSE
           IF Field.Value = 'MENSAL + KILO' THEN
             BEGIN
              Background  := clDkGray;
              AFont.Style := AFont.Style + [fsBold];
              AFont.Color := clBlack;
             END ELSE
           IF Field.Value = 'TAXA RECOLHIMENTO + QUILO' THEN
             BEGIN
              Background  := clGreen;
              AFont.Style := AFont.Style + [fsBold];
              AFont.Color := clBlack;
             END ELSE
           IF Field.Value = 'POR BOMBONA' THEN
             BEGIN
              Background  := clPurple;
              AFont.Style := AFont.Style + [fsBold];
             END;
    end;


  if Field.FieldName = 'SERASA' then
    begin

          if  (Field.Value  = 'S') then
             Begin
               AFont.Style := AFont.Style + [fsBold];
               AFont.Color := clWhite;
               Background  := clRed;
             end else
          if  (Field.Value  = 'N') then
             Begin
               AFont.Style := AFont.Style + [fsBold];
               AFont.Color := clWhite;
               Background  := clBlue;
             end;

    end;

  if Field.FieldName = 'CENTRAL_COBRANCA' then
    begin

          if  (Field.Value  = 'S') then
             Begin
               AFont.Style := AFont.Style + [fsBold];
               AFont.Color := clWhite;
               Background  := clRed;
             end else
          if  (Field.Value  = 'N') then
             Begin
               AFont.Style := AFont.Style + [fsBold];
               AFont.Color := clWhite;
               Background  := clBlue;
             end;

    end;


  if Field.FieldName = 'ISOLAR_BUSCA' then
    begin

          if  (Field.Value  = 'S') then
             Begin
               AFont.Style := AFont.Style + [fsBold];
               AFont.Color := clWhite;
               Background  := clRed;
             end else
          if  (Field.Value  = 'N') then
             Begin
               AFont.Style := AFont.Style + [fsBold];
               AFont.Color := clWhite;
               Background  := clBlue;
             end;

    end;

end;

Exemplo:
select  CR.Cod_conta,CR.Cod_cliente,CR.Data_cad,CR.Vencimento,CR.Saldo,
CR.Dias_atraso,
        CR.Situacao,CR.Vlr_juros, CR.Valor,CR.Cod_reg,
CR.Pago,CR.Total_pago, 
        CR.Desconto,CR.Cod_usuario,CR.Data_pgto, CR.Juros,CR.Obs,
CR.Despesas_extras, CR.DIA_SEMANA, CR.NF, CR.FAR_MTRS,
        CR.BOLETO_EMITIDO,
        CR.KILOS_EXCEDENTES,
        CR.TAXA_RECOLHIMENTO,
        CR.BOMBONAS, CR.Cod_fars_mtrs, CR.serasa, CR.data_serasa,
CR.data_baixa_serasa, CR.Central_Cobranca,
        CR.COD_EDITOR, CR.Ano, CR.Mes, CR.Vcto_ORIGINAL,  CR.JUSTIFICA_VCTO,

        CL.Razao_social as Razao_Social,
        CL.COD_CLIENTE_EMPRESA,
        CL.Fone , CL.Celular,
        CL.Fax , CL.Endereco ,CL.No_casa, CL.Bairro,
        CL.Cep, Cl.Cod_cidade,
        CL.Nome_fantasia, CL.Cpf_cnpj ,cL.Contato,
        CL.Email , CL.Rg_insc, 
        CL.FORMA_PGTO, CL.FECHA_MANUAL, CL.Com_Nota_Fiscal,
        CL.Cod_area_atuacao, CL.COD_GRUPO, CL.taxa_retencao,
CL.ISOLAR_BUSCA,

        CL.endereco_corresp ,CL.no_casa_corresp, CL.bairro_corresp,
CL.cep_corresp, CL.cod_cidade_corresp,

        (SELECT AR.Area FROM Areas_atuacao AR WHERE AR.Cod_area_atuacao =
CL.Cod_area_atuacao)AS AREA_ATUACAO,

        (SELECT CID.CIDADE FROM CIDADES CID WHERE CID.Cod_cidade =
CL.Cod_cidade)AS CIDADE,
        (SELECT CID.Uf FROM CIDADES CID WHERE CID.Cod_cidade =
CL.Cod_cidade)AS UF,

        (SELECT CID.CIDADE FROM CIDADES CID WHERE CID.Cod_cidade =
CL.cod_cidade_corresp)AS CIDADE_corresp,
        (SELECT CID.Uf FROM CIDADES CID WHERE CID.Cod_cidade =
CL.Cod_cidade_corresp)AS UF_corresp


from ctaRec CR,Clientes CL
where 
    CR.Cod_cliente = CL.Cod_cliente and
 (CR.Vencimento between :VarDataI and :VarDataF) and
    Upper(CL.Razao_social) Like Upper(:VarNome) and CR.Deletado = 'N'
order by CR.Vencimento,CR.Cod_cliente, CR.Cod_conta





Mais detalhes sobre a lista de discussão lista