Good afternoon guys.
I need a help, I tried to see in the session above but I could not solve my problem.
My problem is as follows, I need to make a selection of products and all of them separated by vendor, for example.
Imagine that my bank has a product and supplier register, for example, soccer balls, and I have registered 5 balls of the Penalty, 19 of Adidas and 3 of Umbro, I wanted to bring a ball from each supplier. 1 Penalty, 1 Adidas, 1 Umbro.
SELECT
distinct top 3 sup.Name as Supplier, pr.CommercialName
FROM Product pr
FULL OUTER JOIN ProductSupplier AS ps ON (pr.IdProduct = ps.IdProduct)
FULL OUTER JOIN Supplier AS sup ON (ps.IdSupplier = sup.IdSupplier)
WHERE
pr.CASNo in ('516849','99879846','68487498','984987','6510016847') or
pr.IdProduct in ('2270')
But instead of just bringing one product from each vendor, it brings random items in the middle of the bank.
Change -
CREATE TABLE Produto (
id int not null primary key identity(1,1),
nome varchar(100),
);
CREATE TABLE Fornecedor (
id int not null primary key identity(1,1),
nome varchar(100)
);
CREATE TABLE produtoFornecedor(
id int not null primary key identity(1,1),
idProduto int FOREIGN KEY REFERENCES produto(id),
idFornecedor int FOREIGN KEY REFERENCES fornecedor(id));
This is basically my select bank that I tried was this:
SELECT
distinct sup1.nome as Fornecedor,
(
select
top 1 (pr2.nome)
from Produto pr2
inner JOIN Fornecedor AS sup2 ON (ps.idFornecedor = sup2.idFornecedor)
WHERE sup2.nome = sup1.nome
) as produto
FROM Produto pr
FULL OUTER JOIN Produto AS ps ON (pr.idProduto = ps.idProduto)
FULL OUTER JOIN Fornecedor AS sup1 ON (ps.idFornecedor = sup1.idFornecedor)
This is the mass of data that has returned, but it has taken a null and repeated the products for some reason that I can not see