2
I need to make a query that returns a column with the amount of orders that the customer made within the period past in the WHERE and put together another column with the total orders that the customer has made so far. Sort of like this:
[CLIENTE][COMPRAS NO PERÍODO][COMPRAS NO TOTAL]
Marcos 5 28
Flávio 7 35
Roberta 9 32
How would I do that?
Thank you!
EDIT
Here’s the SQL I’m using for the tests. What I really want is the [Total Title] field with the same idea I mentioned above: (Ah, detail: I can’t change the structure of the comic. It’s already deployed)
SELECT CustosVendaItemPed.CodConta
,CustosVendaItemPed.Descricao
,OrdensProducao.NumOrdem 'OP'
,NotasFiscais.DataEmissao 'Data Emissão'
,NotasFiscais.NumNota 'Nota'
,OrcHdr.NomeCliente 'Cliente'
,NotasFiscais.ValorTotalNota 'Valor NF'
,'PercComissao' = CASE WHEN Natureza LIKE 'C' AND OrdensProducao.Cancelado = 0
THEN SUM(Valor/SPreco) END
,'Titulo Período' = COUNT(FIN_Titulos.IdTipoDocumento)
,'Titulo Total' = ???
FROM OrcHdr
INNER JOIN OrdensProducao ON OrcHdr.NumOrdem = OrdensProducao.NumOrdem
INNER JOIN CustosVendaItemPed ON OrdensProducao.NumOrdem = CustosVendaItemPed.NumPedido
INNER JOIN ItemNota ON OrdensProducao.NumOrdem = ItemNota.NumOrdem
INNER JOIN NotasFiscais ON ItemNota.ObjID_Nota = NotasFiscais.ObjID
INNER JOIN FIN_Titulos ON NotasFiscais.ObjID = FIN_Titulos.ObjIDDoctoOrigem
WHERE CustosVendaItemPed.Descricao NOT LIKE 'CIN%'
AND CustosVendaItemPed.Descricao NOT LIKE 'Centro Integrado%'
AND CustosVendaItemPed.Descricao NOT LIKE 'não pagar%'
AND NotasFiscais.Situacao LIKE 'N'
AND ItemNota.Fatura LIKE 'F'
AND ItemNota.Devolucao NOT LIKE 'D'
AND NotasFiscais.NaturezaOperacao NOT LIKE 'reme%'
AND NotasFiscais.NaturezaOperacao NOT LIKE 'doa%'
AND FIN_Titulos.DebCred LIKE 'C'
AND FIN_Titulos.Situacao <> 3
AND DataVencimento >= '2012-11-01 00:00:00.000'
AND DataVencimento <= '2012-11-30 00:00:00.000')
GROUP BY
CustosVendaItemPed.CodConta
,CustosVendaItemPed.Descricao
,OrdensProducao.NumOrdem
,NotasFiscais.DataEmissao
,NotasFiscais.NumNota
,OrcHdr.NomeCliente
,NotasFiscais.ValorTotalNota
,CustosVendaItemPed.Natureza
,OrdensProducao.Cancelado
,DataVencimento
Order By
CustosVendaItemPed.CodConta
,CustosVendaItemPed.Descricao
,OrdensProducao.NumOrdem
It depends on your modeling, present your pro staff structure and the code you’ve achieved so far. But a path would be sub-query’s...
– KaduAmaral
You can show the bank modeling. Where the FKS are.
– Marconi
Wouldn’t you decide to put an underspend on your select? That’s kind of how I proposed.
– Pablo Tondolo de Vargas