I think it might help in future doubts with a similar situation:
Assuming the following data source
datavenda vendedor valor
2015-04-01 FULANO1 700,00
2015-04-02 FULANO1 300,00
2015-04-01 FULANO2 600,00
2015-04-02 FULANO2 200,00
2015-04-01 FULANO3 400,00
2015-04-02 FULANO3 300,00
2015-04-01 FULANO4 30,00
2015-04-02 FULANO4 70,00
2015-04-01 FULANO1 1500,00
2015-04-01 FULANO1 350,00
2015-04-02 FULANO1 750,00
2015-04-02 FULANO1 300,00
2015-04-01 FULANO2 1800,00
2015-04-01 FULANO3 600,00
2015-04-01 FULANO4 800,00
2015-04-03 FULANO1 1500,00
2015-04-03 FULANO2 3000,00
2015-04-04 FULANO4 6000,00
The query below brings the total of each seller between an initial date and a final date.
declare @dt_inicio date
declare @dt_fim date
set @dt_inicio = '2015-04-01'
set @dt_fim = '2015-04-04'
SELECT distinct @dt_inicio as [inicio],
@dt_fim as [fim],
A.vendedor, soma.v as [TOTAL]
from VENDAS AS A
cross apply (
select sum(valor) v from VENDAS
where datavenda between @dt_inicio and @dt_fim
and vendedor = A.vendedor
) soma
where datavenda between @dt_inicio and @dt_fim
order by soma.v DESC
Upshot:
2015-04-01 2015-04-04 FULANO4 6900,00
2015-04-01 2015-04-04 FULANO2 5600,00
2015-04-01 2015-04-04 FULANO1 5400,00
2015-04-01 2015-04-04 FULANO3 1300,00
To increment, we can add the corresponding % to each seller within the searched period.
SELECT distinct @dt_inicio as [inicio],
@dt_fim as [fim],
A.vendedor, soma.v as [TOTAL],
format((soma.v / soma2.v), 'p') as [% sobre TOTAL NO PERÍODO]
from VENDAS AS A
cross apply (
select sum(valor) v from VENDAS
where datavenda between @dt_inicio and @dt_fim
and vendedor = A.vendedor
) soma
cross apply (
select sum(valor) v from VENDAS
where datavenda between @dt_inicio and @dt_fim
) soma2
where datavenda between @dt_inicio and @dt_fim
order by soma.v DESC
Upshot:
2015-04-01 2015-04-04 FULANO4 6900,00 35,93%
2015-04-01 2015-04-04 FULANO2 5600,00 29,16%
2015-04-01 2015-04-04 FULANO1 5400,00 28,12%
2015-04-01 2015-04-04 FULANO3 1300,00 6,77%
I have a habit of always saving dates in American format. To format pro Brazil just use:
...
format(@dt_inicio, 'dd/MM/yyyy')as [inicio],
format(@dt_fim, 'dd/MM/yyyy')as [fim], ...
If you prefer, you don’t have to leave the dates @dt_inicio as [inicio], @dt_fim as [fim]
no select. You can withdraw to return on the lines only the name and total of each seller.
Do you want the total result between two dates? If so, you need to change your SQL slightly. Example:
SELECT VENDEDOR, SUM(TOTAL) AS GERAL FROM VENDAS WHERE DATAVENDA BETWEEN '2014-12-04' AND '2014-12-08' GROUP BY VENDEDOR ORDER BY SUM(TOTAL) DESC;
– Rafael Withoeft
Friend, sorry I’m new here, the more I use is the sqlserver.
– antonio germano
Rafael has already given an error, because select be missing date. select datavenda,vendedor, sum(total) as geral from vendas Where datavenda between '2014-12-04' and '2014-12-08' group by vendedor order by sum(total) desc
– antonio germano
Is there an example of what end result you would like exactly? Something like?
DataVenda: Dia inicial - Dia Final | Total: 500 | Vendedor: XYZ
?– Rafael Withoeft
It cannot be a select where there is another select inside. Type, do the search between dates and then make the total sum of each seller???
– antonio germano
Without knowing what the end result you want, we have no way to help. As you had asked for a result showing only the total and not date by date, I had put it above... but you said you need the date, so... what is the final result expected by you?
– Rafael Withoeft
The result is the total sum of sales of each sales each seller by period. Start date and end date.
– antonio germano
I have no idea if it will work or not, but do a test with this SQL:
SELECT VENDEDOR, CONCAT(MAX(DATAVENDA),' ', MIN(DATAVENDA)) AS DATAS, SUM(TOTAL) AS GERAL FROM VENDAS WHERE DATAVENDA BETWEEN '2014-12-04' AND '2014-12-08' GROUP BY VENDEDOR ORDER BY SUM(TOTAL) DESC;
– Rafael Withoeft
I’ll test it, as soon as it works out here I’ll let you know later today. See you.
– antonio germano
Rafael his first solution worked perfectly. The second failed to execute, but I am already grateful. Obg and until the next colleague.
– antonio germano
It wouldn’t just take the date of the grouping !?
– Motta
How would I look without the motta cluster??
– antonio germano