I have a detail Catalogo
that is called ProductoCatalogo
in which I enter the products that are going to be sold, but there are products that have their specifications, as in the case of clothing they have: Size and Color, that is registered in a Specifications master that has the following structure: Header = table Especificaciones
and Detail = Table Opciones
in Especificaciones
va Color and in Opciones
va Red, Blue, Yellow, etc. The same happens for sizes.
This is the structure of the tables involved to get the information I want.
The information I want to obtain is the following:
- ProductCatalogId
- Product name
- Sale price
- Size
- Color
This is what I expect as a result:
1 CAMISETA FERRARI 12.00 TALLA LETRA S COLOR ROJO
1 CAMISETA FERRARI 12.00 TALLA LETRA S COLOR AZUL
For that I have elaborated the following query in which I apply a filter by the name of the product. As I told you, I have a master specification that is why in OptionName I get the sizes and colors.
SELECT PC.ProductoCatalogoId, PC.ProductoId, P.Nombre, PC.PrecioVenta,
EP.EspecificacionId, E.Nombre, EC.OpcionId,
O.NombreOpcion
FROM ProductoCatalogos AS PC
JOIN Productos AS P ON P.ProductoId = PC.ProductoId
JOIN EspecificacionProductos AS EP ON EP.ProductoId = P.ProductoId
JOIN Especificaciones AS E ON E.EspecificacionId = EP.EspecificacionId
JOIN EspecificacionCatalogos AS EC ON EC.EspecificacionProductoId = EP.EspecificacionProductoId
JOIN Opciones AS O ON O.OpcionId = EC.OpcionId
WHERE P.Nombre LIKE '%FERR%'
Which gives me the following data.
I need to separate the sizes and the colors for that I have improvised with PIVOT
, with which it has been possible to put the colors in the Color column which was separated with PIVOT
, but it has not worked with Size.
WITH Pruebas(ProductoCatalogoId, ProductoId, NombreProducto, PrecioVenta,
EspecificacionId, Nombre, OpcionId, NombreOpcion) AS
(SELECT PC.ProductoCatalogoId, PC.ProductoId, P.Nombre, PC.PrecioVenta, EP.EspecificacionId, E.Nombre, EC.OpcionId,
O.NombreOpcion
FROM ProductoCatalogos AS PC
JOIN Productos AS P ON P.ProductoId = PC.ProductoId
JOIN EspecificacionProductos AS EP ON EP.ProductoId = P.ProductoId
JOIN Especificaciones AS E ON E.EspecificacionId = EP.EspecificacionId
JOIN EspecificacionCatalogos AS EC ON EC.EspecificacionProductoId = EP.EspecificacionProductoId
JOIN Opciones AS O ON O.OpcionId = EC.OpcionId
WHERE P.Nombre LIKE '%FERR%')
SELECT * FROM PRUEBAS
PIVOT(MAX(NombreOpcion) FOR Nombre IN ([TALLA LETRA], [COLOR])) PVT
It gives me the following information.
trying on pants
I don't know if it's the right way or if there is another way to get what I need. Credentials to connect to the database.
Server: den1.mssql4.gear.host User: business Pass: Ga33-47L1YC!
Thanks in advance. Cheers!
If you only want to show those 2 products, already arranged by their colors and sizes, it would look something like this:
In this case you must specify the accommodation of your new field, for this you must define which specificationId = 2 (COLOR) and specificationId = 5 (SIZE LETTER) for your shirt.
I hope and serve you.