3
I need to make a query in sql server where I have to "connect" stocks to companies.
The stock has 'n' products attached to it, and these products have 1 category linked to it.
The company also has 'n' categories related to it.
I need to select all stocks that have at least 1 product that the category is equal to one of the company categories.
For example:
Stocking 1
produto 1 - categoria: pedra
produto 2 - categoria: brita
produto 3 - categoria: areia
Enterprise 1
categorias: concreto, pedra, cimento.
Enterprise 2
categorias: cimento, tijolo.
In this case, only company 1 would view stock 1, because one of its categories matches the category of one of the products within the stock.
Table Category
IDCATEGORIA
NMCATEGORIA
Stock Table
IDESTOQUE
NMESTOQUE
Product Table
IDPRODUTO
IDESTOQUE
NMPRODUTO
IDCATEGORIA
Table Company
IDEMPRESA
NMEMPRESA
Table Empresacategoria
IDEMPRESA
IDCATEGORIA
I hope I was clear.
Thanks in advance.
Diego, your question is fine, but without knowing how the tables are and the relationships gets a little complicated to assemble a query and help you! try to place the tables here, at least the part (that interests you) . abs.
– Thiago Loureiro
Diego the product categories in the tables
empresa 1
andempresa 2
have the samecodigo
that in stock table?– R.Santos
Put the structure of the tables, so we can help!
– Anderson Souza
The category is the same table. I edited the question, if you need more data just say it. Think about using the "IN", but I’ve only used it to date from a variable in a group, not group to group.
– Diego Sousa
Maybe I can help you.
– Renato Junior
An ERI would help to see the relationships between the tables.
– Renato Junior