<small><pre>
SELECT 
    vidquoten2.VidQuoteID AS ID,
    vidquoten2.VidQuoteItem AS Item,
    vidquoten2.VidQuoteProdCantidad AS Cant,
    vidquoten2.VidQuoteProdID AS ProdID,
    CASE
        WHEN `vidquoten2`.`VidQuoteAlto` > 0 THEN
            CONCAT('Dimension(mm): [',
            LPAD(CAST(`vidquoten2`.`VidQuoteAncho` AS CHAR),4,'0000'),'x',
            LPAD(CAST(`vidquoten2`.`VidQuoteAlto` AS CHAR),4,'0000'),']')
        WHEN `vidquoten2`.`VidQuoteAlto` > 0 THEN
            CONCAT('Largo(mm): [',
            LPAD(CAST(`vidquoten2`.`VidQuoteAncho` AS CHAR),4,'0000'),']')
        ELSE
            ''
    END AS Dimension,
    vidquoten2.VidQuotePrecio AS Precio,
    vidquoten1.VidQuoteFecha AS Fecha,
    vidquoten1.VidQuoteCliente AS Cliente,
    vidquoten3.VidQuoteElementoID AS ElementoID,
    vidquoten3.VidQuoteComposicID AS ComposicID,
    vidquoten3.VidQuoteIncluyeDet AS Incluye,
    productos.Descripcion AS DesG,
    IF(vidquoten3.VidQuoteIncluyeDet = 1,
        productos_1.Descripcion,
        '') AS DesP
FROM
    vidquoten1 AS vidquoten1,
    vidquoten2 AS vidquoten2,
    vidquoten3 AS vidquoten3,
    productos AS productos,
    productos AS productos_1
WHERE
    vidquoten3.VidQuoteIncluyeDet = 1 AND 
    vidquoten1.VidQuoteID = vidquoten2.VidQuoteID AND 
    vidquoten3.VidQuoteID = vidquoten2.VidQuoteID AND 
    vidquoten3.VidQuoteItem = vidquoten2.VidQuoteItem AND 
    productos.ProductoID = vidquoten3.VidQuoteProdID AND 
    productos_1.ProductoID = vidquoten3.VidQuoteElementoID AND 
    vidquoten2.VidQuoteID = 50

UNION
SELECT 
    vidquoten2.VidQuoteID AS ID,
    vidquoten2.VidQuoteItem AS Item,
    vidquoten2.VidQuoteProdCantidad AS Cant,
    vidquoten2.VidQuoteProdID AS ProdID,
    CASE
        WHEN `vidquoten2`.`VidQuoteAlto` > 0 THEN
            CONCAT('Dimension(mm): [',
            LPAD(CAST(`vidquoten2`.`VidQuoteAncho` AS CHAR),4,'0000'),'x',
            LPAD(CAST(`vidquoten2`.`VidQuoteAlto` AS CHAR),4,'0000'),']')
        WHEN `vidquoten2`.`VidQuoteAlto` > 0 THEN
            CONCAT('Largo(mm): [',
            LPAD(CAST(`vidquoten2`.`VidQuoteAncho` AS CHAR),4,'0000'),']')
        ELSE
            ''
    END AS Dimension,
    vidquoten2.VidQuotePrecio AS Precio,
    vidquoten1.VidQuoteFecha AS Fecha,
    vidquoten1.VidQuoteCliente AS Cliente,
    vidquoten3.VidQuoteElementoID AS ElementoID,
    vidquoten3.VidQuoteComposicID AS ComposicID,
    vidquoten3.VidQuoteIncluyeDet AS Incluye,
    productos.Descripcion AS DesG,
    IF(vidquoten3.VidQuoteIncluyeDet = 1,
        productos_1.Descripcion,
        '') AS DesP
FROM
    vidquoten1 AS vidquoten1,
    vidquoten2 AS vidquoten2,
    vidquoten3 AS vidquoten3,
    productos AS productos,
    productos AS productos_1
WHERE
    vidquoten2.VidQuoteID = 50 AND
    vidquoten3.VidQuoteIncluyeDet = 0 AND
    vidquoten1.VidQuoteID = vidquoten2.VidQuoteID AND 
    vidquoten3.VidQuoteID = vidquoten2.VidQuoteID AND 
    vidquoten3.VidQuoteItem = vidquoten2.VidQuoteItem AND 
    productos.ProductoID = vidquoten3.VidQuoteProdID AND 
    productos_1.ProductoID = vidquoten3.VidQuoteElementoID AND
    vidquoten2.VidQuoteItem NOT IN (
        SELECT 
            vidquoten2.VidQuoteItem AS Item
        FROM
            vidquoten2 AS vidquoten2,
            vidquoten3 AS vidquoten3
        WHERE
            vidquoten3.VidQuoteIncluyeDet = 1 AND
            vidquoten3.VidQuoteID = vidquoten2.VidQuoteID AND 
            vidquoten3.VidQuoteItem = vidquoten2.VidQuoteItem AND 
            vidquoten2.VidQuoteID = 50
        GROUP BY
            vidquoten2.VidQuoteItem)
</pre></small>