I need to make a report that brings the last sale of each product being in SQL

Asked

Viewed 39 times

0

In this report I need to bring only the field with the last output of product X with the respective fields code, desc, client and etc have to do so, but the fields are repeated

SELECT distinct 
nts.not_numero,
nts.not_danfe_numero                                                          AS NF,
--CONVERT(VARCHAR(10), nts.not_dat_emissao, 103)                                AS DATA_NF,
(SELECT TOP 1 CONVERT(VARCHAR(10),CAST(MAX(CONVERT(VARCHAR(10),nts.not_dat_emissao,112)) AS DATETIME),103)  
FROM tbl_notas nts WHERE pro.pro_codigo = mov.pro_codigo )                    AS DATA_SAIDA,
cli.cli_razao_social                                                          AS RAZAO_SOCIAL,
pro.pro_codigo                                                                AS CODIGO_PRODUTO,
(pro_descricao + ' ' + pro_apresentacao)                                      AS DESCRICAO,
pro.pro_codigo_barras                                                         AS EAN,
pro.pro_prc_venda                                                             AS PRECO_VENDAS,
pro_prc_ult_compra                                                            AS PRECO_ULTIMA_COMPRA,
PRO_PERC_CUSTO_OPERACIONAL, 
(pro_prc_ult_compra * pro_perc_custo_operacional) / 100                       AS VLR_PERC_CUST_OP,
PRO_PERC_ST_ESPECIAL,   
(pro_prc_ult_compra * PRO_PERC_ST_ESPECIAL)/100 AS VLR_ST_ESPECIAL,
pro_per_desc_custo_ven as PERC_DESC_CUST_FARM    
 FROM tbl_produtos pro
 INNER JOIN tbl_moviment_pro mov ON pro.pro_codigo = mov.pro_codigo    ---tbl corpos da nota fisca 
 INNER JOIN tbl_notas nts ON nts.not_numero = mov.mov_numero_nf  ---tbl cabeçario da Nota fiscal 
 INNER JOIN tbl_cliente cli ON cli.cli_codigo = nts.cli_codigo  ---tbl cliente  
  • If the last sale is the one on the latest date then use the GROUP BY product clause and the MAX aggregation function (data_da_sale).

  • ... where nts.not_dat_emissao = (select max(nts.not_dat_emissao) from tbl_notas nts2 where nts2.cli_codigo = nts.cli_codigo) ... algo assim sem o subselect na seleção

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.