Ir para conteúdo
Fórum Script Brasil

ROMANVPP

Membros
  • Total de itens

    5
  • Registro em

  • Última visita

Sobre ROMANVPP

ROMANVPP's Achievements

0

Reputação

  1. Pessoal, Tenho a seguinte query: select *, totvalorliquido - totvalorcustorep as lucro from(select distinct tp.margembrutalucro,sum(ttm.quantidade * ttm.precounitario)as totvalorliquido, SUM(case when tp.margembrutalucro >= 1.15 and tp.margembrutalucro <= 1.30 then (ttm.precounitario*ttm.quantidade) * 0.01 when tp.margembrutalucro >= 1.31 and tp.margembrutalucro <= 1.50 then (ttm.precounitario*ttm.quantidade) * 0.012 else cast(tp.margembrutalucro as varchar) end) as totmargemnota, sum(ttm.quantidade * tp.custoreposicao)as totvalorcustorep, SUM(case when tp.margembrutalucro >= 1.15 and tp.margembrutalucro <= 1.30 then (tp.custoreposicao*ttm.quantidade) * 0.01 when tp.margembrutalucro >= 1.31 and tp.margembrutalucro <= 1.50 then (tp.custoreposicao*ttm.quantidade) * 0.012 else cast(tp.margembrutalucro as varchar) end) as totmargemprecocheio from tprd tp with (nolock) inner join titmmov ttm on (ttm.idprd = tp.idprd) inner join tmov tm on (tm.idmov = ttm.idmov) where tm.dataemissao >= '2011/11/01' and tm.dataemissao <='2011/11/01' and tm.CODTMV IN ('2.2.01','2.2.29') AND tm.codcfo not in('000337') and tm.STATUS <> 'C' group by tp.margembrutalucro)t1 group by margembrutalucro, totvalorliquido,totvalorcustorep, totmargemnota, totmargemprecocheio order by margembrutalucro O resultado é esse: Margem TotValorLiquido TotMargemNota TotValorCustoRep TotMargemPrecoCheio Lucro 1.2000 5542.17000000 55.42170000000 4451.71300000 44.51713000000 1090.45700000 1.2500 1945.53000000 19.45530000000 1447.97020000 14.47970200000 497.55980000 1.2700 3.80000000 .03800000000 3.00000000 .03000000000 .80000000 1.3000 200.20000000 2.00200000000 153.60500000 1.53605000000 46.59500000 1.5000 2261.45000000 27.13740000000 1510.72060000 18.12864720000 750.72940000 Preciso criar um campo chamado PERCENTUAL que pegue o campo TOTVALORLIQUIDO e compare com o somatório do campo "TotValorLiquido" encontrando o seu valor percentual em relação ao somatório, ou seja o resultado seria: Margem TotValorLiquido Percentual TotMargemNota TotValorCustoRep TotMargemPrecoCheio Lucro 1.2000 5542.17000000 55,6825 55.42170000000 4451.71300000 44.51713000000 1090.45700000 1.2500 1945.53000000 19,5438 19.45530000000 1447.97020000 14.47970200000 497.55980000 1.2700 3.80000000 0,0381 .03800000000 3.00000000 .03000000000 .80000000 1.3000 200.20000000 2,011 2.00200000000 153.60500000 1.53605000000 46.59500000 1.5000 2261.45000000 22,72 7.13740000000 1510.72060000 18.12864720000 750.72940000 Agradeço caso alguém tenha uma idéia.
  2. ROMANVPP

    query analyser

    FalaFulvio, Cara eu fiz um backup do banco e restaurei no outro servidor. Os índices estão iguais. Ainda não sei o q pode ser. Vlw
  3. Boa tarde a todos, Tenho uma consulta que roda em um de meus servidores no query analyser, mas em outro de teste não roda. Simplesmente fica executando mas não retorna o resultado. Os dois servidores são windows 2003 server / SQL Server 2000. Abaixo a query: SELECT FCFO.email,FCFO.CGCCFO,FCFO.telefone,FCFO.rua,FCFO.numero,FCFO.CEP,FCFO.complemento, FCFO.bairro,FCFO.cidade,FCFO.codcfo,FCFO.NOME,FCFO.USUARIOCRIACAO,DTEMISSAO, NMOV,fc.mala_direta FROM FCFO WITH (NOLOCK) LEFT JOIN FCFOCOMPL FC ON (FC.CODCFO = FCFO.CODCFO) INNER JOIN FCFODEF ON (FCFO.CODCFO = FCFODEF.CODCFO) LEFT JOIN (select codcfo,max(dataemissao) AS DTEMISSAO, MAX(NUMEROMOV) as NMOV from tmov WITH (NOLOCK) group by codcfo)T1 ON (T1.CODCFO = FCFO.CODCFO),TMOVCOMPL WHERE FCFODEF.CODVEN='0005' GROUP BY FCFO.email,FCFO.CEP,FCFO.CGCCFO,FCFO.telefone,FCFO.rua,FCFO.numero,FCFO.complemento, FCFO.bairro,FCFO.cidade,FCFO.USUARIOCRIACAO,FCFO.codcfo,FCFO.NOME,DTEMISSAO, NMOV, FC.mala_direta ORDER BY FCFO.NOME PS: se eu removo o "LEFT JOIN (select codcfo,max(dataemissao) AS DTEMISSAO, MAX(NUMEROMOV) as NMOV from tmov WITH (NOLOCK) group by codcfo)T1" funciona. Pode ser algum parâmetro do sql server talvez. Obrigado
  4. Boa tarde pessoal, Tenho essa consulta que me retorna 353 registros: select distinct di.idlaf,di.numitem,dic.natrec from dlaf dl inner join ditem di on (di.idlaf = dl.idlaf) left join ditemcompl dic on (dic.idlaf = di.idlaf and di.numitem = dic.numitem) inner join tprd tp on (tp.idprd = di.idprd) inner join dtrbitem dti on (dti.idlaf = di.idlaf and dti.numitem = di.numitem) where tp.descricao like '%arroz%' and dl.tipolf = 'S' and dl.dataemissao >= '2011/08/01' and dl.dataemissao <= '2011/08/31' and dti.sittributaria in ('04','06') and dl.seriedoc in ('1','ECF') and dic.natrec is null order by di.idlaf Preciso atualizar o campo dic.natrec desses 353 registros. Fiz assim: update ditemcompl set natrec = '105' from dlaf dl inner join ditem di on (dl.idlaf = di.idlaf) left join ditemcompl dic on (dic.idlaf = di.idlaf and dic.numitem = di.numitem) inner join tprd tp on (tp.idprd = di.idprd) inner join dtrbitem dti on (dti.idlaf = di.idlaf and dti.numitem = di.numitem) where tp.descricao like '%arroz%' and dl.tipolf = 'S' and dl.dataemissao >= '2011/08/01' and dl.dataemissao <= '2011/08/31' and dti.sittributaria in ('04','06') and dl.seriedoc in ('1','ECF') and dic.natrec is null Atualizou apenas 196 registros. Ou seja como se a query buscasse apenas os idlaf (identificador do lanc. fiscal) sem o numitem que seriam os items do lançamento fiscal (totalizando 353 registros). A forma como estou aplicando o update deve estar incorreta. Se puderem dar uma força eu agradeço.
  5. ROMANVPP

    update + join

    Pessoal, Alguém sabe como fazer um update em uma query como essa? No caso preciso atualizar apenas o campo "natrec". select distinct dl.docini,dl.seriedoc,dl.idlaf,dl.dataemissao,di.idprd,tp.descricao,dic.natrec,dti.sittributaria from dlaf dl inner join ditem di on (di.idlaf = dl.idlaf) left join ditemcompl dic on (dic.idlaf = di.idlaf and di.numitem = dic.numitem) inner join tprd tp on (tp.idprd = di.idprd) inner join dtrbitem dti on (dti.idlaf = di.idlaf and dti.numitem = di.numitem) where tp.descricao like '%arroz%' and dl.tipolf = 'S' and dl.dataemissao >= '2011/07/01' and dl.dataemissao <= '2011/07/31' and dti.sittributaria in ('04','06') and dl.seriedoc in ('1','ECF') and dic.natrec is null order by tp.descricao Tentei dessa forma mas não funciona (não pega todos os registros que preciso): update ditemcompl set natrec = '105' from ditemcompl dic inner join ditem di on (dic.idlaf = di.idlaf and di.numitem = dic.numitem) inner join dlaf dl on (di.idlaf = dl.idlaf) inner join tprd tp on (tp.idprd = di.idprd) inner join dtrbitem dti on (dti.idlaf = di.idlaf and dti.numitem = di.numitem) where tp.descricao like '%arroz%' and dl.tipolf = 'S' and dl.dataemissao >= '2011/07/01' and dl.dataemissao <= '2011/07/31' and dti.sittributaria in ('04','06') and dl.seriedoc in ('1','ECF') and dic.natrec is null Se alguém puder ajudar fico grato
×
×
  • Criar Novo...