As it stands on the table vendas
There is no way to know which team the seller was on when the sale was held, since he can change teams after making the sale. The simplest solution to this (there are others) would simply be to add a field id_equipe
on the table vendas
which corresponds to the team in which the seller was at the time of the sale.
Also, there is no table vendas
nothing indicating when a particular sale took place, so it is not clear which ones were sold in the first 15 days of January or the like. The most obvious solution to this would be to add a field data_venda
on the table vendas
of the kind timestamp
.
Consultation behind products sold by a particular seller in a given period:
SELECT v.id, v.data_venda, vd.nome AS nome_vendedor, e.nome AS nome_equipe, v.produto
FROM vendas v
INNER JOIN vendedor vd ON v.id_vendedor = vd.id
INNER JOIN equipe e ON v.id_equipe = e.id
WHERE v.id_vendedor = :id_vendedor
AND v.data_venda BETWEEN :limite_inferior AND :limite_superior
Where :id_vendedor
is the id of the seller from whom one wishes to obtain the sales made and :limite_inferior
and :limite_superior
is the period to be considered.
Query showing the amount of products sold per team in a given period:
SELECT e.id, e.nome, COUNT(v.id) AS qtd, v.id_equipe
FROM vendas v
INNER JOIN equipe e ON v.id_equipe = e.id
WHERE v.data_venda BETWEEN :limite_inferior AND :limite_superior
GROUP BY v.id_equipe
Where :limite_inferior
and :limite_superior
is the period to be considered.
Query showing the amount of products sold per team and per seller in a given period:
SELECT e.id AS id_equipe, e.nome AS nome_equipe, vd.id AS id_vendedor, vd.nome AS nome_vendedor, COUNT(v.id) AS qtd, v.id_equipe, v.id_vendedor
FROM vendas v
INNER JOIN vendedor vd ON v.id_vendedor = vd.id
INNER JOIN equipe e ON v.id_equipe = e.id
WHERE v.data_venda BETWEEN :limite_inferior AND :limite_superior
GROUP BY v.id_equipe, vd.id
Where :limite_inferior
and :limite_superior
is the period to be considered.
Rodrigo could not leave the team table being informed in the table Seller and Sale?
– Jorge Toledo
@Jorgetoledo If you link the team to the seller, you’ll tell them what the seller’s current team is. If you link the team table to sale, it will tell you which team the seller was at the time of the sale. You can still make both links at the same time, if you prefer, all approaches are valid.
– Rodrigo Rigotti
Okay, Rodrigo, thank you so much I’ll do here.
– Jorge Toledo