¿Y te serviría algo así?
+------------+--------+-----------+----------+
| productoID | nombre | tipoF | Subtotal |
+------------+--------+-----------+----------+
| 1 | TV | Factura | 90 |
| 2 | DVD | Remisión | 16 |
| 2 | DVD | OS | 16 |
| 3 | PC | Remisión | 10 |
| 3 | PC | Factura | 10 |
| 4 | LAPTOP | Remisión | 15 |
| 5 | CELL | Remisión | 5 |
| 6 | MICRO | OS | 18 |
+------------+--------+-----------+----------+
Para conseguirlo tendrías que lanzar la siguiente consulta:
SELECT productoID,nombre,tipoF,Subtotal FROM (
SELECT productoID,nombre tipoF,
SUM(cantidad) Subtotal
FROM (
SELECT productoID,tipoF,
SUM(cantidad) cantidad
FROM venta
GROUP BY 1,2
) c1 JOIN tipoF ON c1.tipoF=tipo
GROUP BY 1,2
) c2 JOIN producto ON productoID=idproducto
ORDER BY 1,2;
Si necesitas que quede en horizontal, puedes pivotar la tabla con un GROUP_BY
:
SELECT productoID,nombre,
SUM(Subtotal) Total,
GROUP_CONCAT(tipoF) Tipos,
GROUP_CONCAT(Subtotal) Subtotales
FROM (
SELECT productoID,nombre,tipoF,Subtotal FROM (
SELECT productoID,nombre tipoF,
SUM(cantidad) Subtotal
FROM (
SELECT productoID,tipoF,
SUM(cantidad) cantidad
FROM venta
GROUP BY 1,2
) c1 JOIN tipoF ON c1.tipoF=tipo
GROUP BY 1,2
) c2 JOIN producto ON productoID=idproducto
) c3 GROUP BY 1,2;
Obteniendo:
+------------+--------+-------+-------------------+------------+
| productoID | nombre | Total | Tipos | Subtotales |
+------------+--------+-------+-------------------+------------+
| 1 | TV | 90 | Factura | 90 |
| 2 | DVD | 32 | Remisión,OS | 16,16 |
| 3 | PC | 20 | Remisión,Factura | 10,10 |
| 4 | LAPTOP | 15 | Remisión | 15 |
| 5 | CELL | 5 | Remisión | 5 |
| 6 | MICRO | 18 | OS | 18 |
+------------+--------+-------+-------------------+------------+
Tal vez si todas las filas tuvieran el mismo número de columnas, te resultaría más útil. Habría que hacer un producto cartesiano tal que así:
SELECT productoID,producto,
SUM(subtotal) Total,
GROUP_CONCAT(tipo ORDER BY tipoF) tipos,
GROUP_CONCAT(subtotal ORDER BY tipoF) subtotal
FROM (
SELECT productoID,tipoF,
IFNULL(subtotal,0) subtotal
FROM (
SELECT * FROM (
SELECT tipo tipoF FROM tipoF
) c1 JOIN (
SELECT DISTINCT productoID FROM venta
) c2
) c3 LEFT JOIN (
SELECT productoID,tipof,
SUM(cantidad) subtotal
FROM venta
GROUP BY 1,2
) c4 USING(productoID,tipoF)
) c5 JOIN (
SELECT idproducto productoID,
nombre producto
FROM producto
) producto USING(productoID)
JOIN (
SELECT tipo tipoF,nombre tipo
FROM tipoF
) tipoF USING(tipoF)
GROUP BY productoID;
Obteniendo:
+------------+----------+-------+----------------------+----------+
| productoID | producto | Total | tipos | subtotal |
+------------+----------+-------+----------------------+----------+
| 1 | TV | 90 | Factura,Remisión,OS | 90,0,0 |
| 2 | DVD | 32 | Factura,Remisión,OS | 0,16,16 |
| 3 | PC | 20 | Factura,Remisión,OS | 10,10,0 |
| 4 | LAPTOP | 15 | Factura,Remisión,OS | 0,15,0 |
| 5 | CELL | 5 | Factura,Remisión,OS | 0,5,0 |
| 6 | MICRO | 18 | Factura,Remisión,OS | 0,0,18 |
+------------+----------+-------+----------------------+----------+
Añadiendo un nuevo tipo:
INSERT INTO tipoF (tipo, nombre)
VALUES (4,'Test');
Obtendríamos este resultado:
+------------+----------+-------+---------------------------+-----------+
| productoID | producto | Total | tipos | subtotal |
+------------+----------+-------+---------------------------+-----------+
| 1 | TV | 90 | Factura,Remisión,OS,Test | 90,0,0,0 |
| 2 | DVD | 32 | Factura,Remisión,OS,Test | 0,16,16,0 |
| 3 | PC | 20 | Factura,Remisión,OS,Test | 10,10,0,0 |
| 4 | LAPTOP | 15 | Factura,Remisión,OS,Test | 0,15,0,0 |
| 5 | CELL | 5 | Factura,Remisión,OS,Test | 0,5,0,0 |
| 6 | MICRO | 18 | Factura,Remisión,OS,Test | 0,0,18,0 |
+------------+----------+-------+---------------------------+-----------+
tipoF
es una tabla, tipof es una columna en la tablaventa
que hace referencia a la columna tipo detipoF
.