I have a code that aims to return open invoices from a customer in a specific time period. But I want to add a field that displays the total number of invoices, regardless of the debit period.
The code looks like this:
SELECT
fatura.num_cli "Cliente", -- Código do cliente
fatura.num_doc "Número do documento", -- Número da NF
fatura.dta_venc "Vencimento", -- Data de vencimento
cliente.cod_situ "Situação" -- Situação do cliente, está em outra tabela
FROM fatura
LEFT OUTER JOIN cliente ON fatura.num_cli = cliente.cod_cliente
WHERE fatura.cod_situ_com = 'AB' -- Situação da fatura - aberto
AND trunc(fatura.dta_venc) between '01-mai-2018' and '31-mai-2018' -- Período de vencimento
But in addition, I wanted to add a field that would return the count of the number of open invoices, regardless of the due date. The structure of the tables is as follows:
Invoice:num_doc | num_cli | dta_venc | cod_situ
01 | 12225 | 01-Mar-2018 | AB Client:
customer_code | nom_cli | cpf | cod_situ
1201 | João | 00000000000 | judged
In this case, each customer can have multiple invoices, and each invoice is linked to a customer.
The code should return the Customer Number; the total open invoices of this customer, regardless of the period (this would be the sub select); the open invoice code and the invoice maturity (both of these within the established period).