I have a detail of 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 the clothes they have: Size and Color, that is registered in a Master of Specifications that has the following structure:
Header = table Especificaciones
and Detail = Table Opciones
in Especificaciones
va Color and in Opciones
goes 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 for the name of the product. As I said, I have a master specifications, that's why in NombreOpcion 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%'
That throws me the following data.
I need to separate the sizes and colors for that I have improvised with PIVOT
, with which it was possible to put the colors in the Color column which 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
He throws me the following information.
Testing with pants
I do not know if it's the right way or 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. Greetings!