Consultation of Total General

Asked

Viewed 23 times

0

I am with the need to build a query where I have the following situation

Table 1- Sales

 [1]: https://i.stack.imgur.com/6GOO7.png

In this table I have other vendor codes with other values. I am trying to bring a new column where I bring the sum of all sales that month, without taking into account the seller filter I apply in the WHERE.

I tried to use the sum(over paritition) but it brought me the total amount of the month considering the seller’s filter.

I just need the help of how to make this VENDAS_T not take into account the Cod_vend filter, but rather the YEAR and MES of the line

DECLARE 
@Cod_Vend int,@Data date SET @Cod_Vend=9 SET    @Data = getdate()

SELECT 
    LTRIM(RTRIM(t.APELIDO)) AS 'VENDEDOR',
    ad.ANO, 
    ad.MES, 
    CONCAT(ad.ANO,'/',ad.MES) DATA,
    SUM(ad.VENDA) VENDA_I, 
    SUM(SUM (ad.VENDA)) OVER (partition by ad.MES ORDER BY ad.MES) AS Vendas_T,
    SUM(ad.DEVOLUCAO) DEVOLUCAO_I
FROM sankhya.ANALISE_DEVOLUCAO2 ad
JOIN sankhya.TGFVEN t ON ad.COD_VENDEDOR=t.CODVEND
WHERE ad.COD_VENDEDOR=@Cod_Vend
AND t.ATIVO='S'
AND ad.ANO=year(@Data)
AND ad.MES >=month(@Data)-6
GROUP BY 
    t.APELIDO,
    ad.ANO, 
    ad.MES`insira o código aqui

`

  • 1

    And why don’t you do the query to take the total, save in a variable and only shows in a column in the new query?

  • 1

    'Cause it’s @Sorack I thought about this early morning out of nowhere... I just made a subselect aggregating the values as I wanted and put it in a Join.

No answers

Browser other questions tagged

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