Creación de informes de un taller utilizando MySQL
Importar la tabla de venta de coches.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
/* Navicat Premium Data Transfer Source Server : maviservicioslocal Source Server Type : MySQL Source Server Version : 50509 Source Host : localhost Source Database : BBDDcoches Target Server Type : MySQL Target Server Version : 50509 File Encoding : utf-8 Date: 02/07/2012 23:40:17 PM */ SET NAMES utf8; SET FOREIGN_KEY_CHECKS = 0; -- ---------------------------- -- Table structure for `coche` -- ---------------------------- DROP TABLE IF EXISTS `coche`; CREATE TABLE `coche` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `matricula` varchar(8) DEFAULT NULL, `marca` varchar(11) DEFAULT NULL, `modelo` varchar(50) DEFAULT NULL, `color` varchar(11) DEFAULT NULL, `tipo` varchar(10) DEFAULT NULL, `Fmatricuka` date DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=latin1; -- ---------------------------- -- Records of `coche` -- ---------------------------- BEGIN; INSERT INTO `coche` VALUES ('1', '0001AAA', 'Seat', 'Ibiza 1.6', 'Aul', 'Turismo', '2008-02-07'), ('2', '0002AAA', 'BMW', 'X5', 'Gris', '4x4', '1998-02-28'), ('3', '0003AAA', 'BMW', '520', 'Azul', 'Berlina', '1980-02-28'), ('4', '0004AAA', 'Ford', 'Mondeo', 'Gris', 'Turismo', '2000-02-28'), ('5', '0005AAA', 'Seat', 'Toledo', 'Blanco', 'Turismo', '2000-02-28'), ('6', '0006AAA', 'Mercedes', 'C330', 'Negro', 'Berlina', '1957-02-28'), ('7', '0007AAA', 'Seat', 'Malaga', 'Amarillo', 'Turismo', '1700-02-28'), ('8', '0008AAA', 'Peugeot', '505', 'Verde', 'Turismo', '1950-02-28'), ('9', '0009AAA', 'Fiat', 'Punto', 'Rojo', 'Turismo', '2003-02-28'), ('10', '0010AAA', 'Citroen', 'Sara', 'Blanco', 'Turismo', '2007-02-28'); COMMIT; -- ---------------------------- -- Table structure for `mantenimiento` -- ---------------------------- DROP TABLE IF EXISTS `mantenimiento`; CREATE TABLE `mantenimiento` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `matricula` varchar(8) DEFAULT NULL, `kilometros` int(11) DEFAULT NULL, `itv` date DEFAULT NULL, `revision` date DEFAULT NULL, `correa` date DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=latin1; -- ---------------------------- -- Records of `mantenimiento` -- ---------------------------- BEGIN; INSERT INTO `mantenimiento` VALUES ('1', '0001AAA', '140000', '2010-06-09', '2010-09-09', '2010-09-08'), ('2', '0002AAA', '25000', '2009-09-09', '2005-10-25', '2004-02-02'), ('3', '0003AAA', '35000', '2011-01-01', '2010-06-06', '2008-06-08'), ('4', '0004AAA', '60000', '2010-02-07', '2011-06-06', '2009-08-07'), ('5', '0005AAA', '100000', '2009-05-05', '2011-06-06', '2008-06-06'), ('6', '0006AAA', '95000', '2010-06-06', '2010-06-06', '2007-08-08'), ('7', '0007AAA', '78000', '2009-06-08', '2009-09-09', '2006-06-08'), ('8', '0008AAA', '74000', '2010-06-06', '2008-07-08', '2009-04-04'), ('9', '0009AAA', '250000', '2010-06-07', '2007-06-08', '2009-07-07'), ('10', '0010AAA', '25000', '2010-06-05', '2007-06-07', '2011-09-09'); COMMIT; -- ---------------------------- -- Table structure for `personas` -- ---------------------------- DROP TABLE IF EXISTS `personas`; CREATE TABLE `personas` ( `id_nCliente` int(11) unsigned NOT NULL AUTO_INCREMENT, `nombre` varchar(20) DEFAULT NULL, `apellidos` varchar(30) DEFAULT NULL, `DNI` varchar(9) DEFAULT NULL, `direccion` varchar(100) DEFAULT NULL, `poblacion` varchar(20) DEFAULT NULL, `provincia` varchar(11) DEFAULT NULL, PRIMARY KEY (`id_nCliente`) ) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=latin1; -- ---------------------------- -- Records of `personas` -- ---------------------------- BEGIN; INSERT INTO `personas` VALUES ('1', 'Juan', 'Gonzalez', '36112525f', 'C/La fuente 25', 'Ajalvir', 'Madrid'), ('2', 'Pedro', 'Perez', '25145878g', 'C/gallo 33', 'Algete', 'Madrid'), ('3', 'Rodrigo', 'Alonso', '36989852g', 'C/nicaragua 36', 'Vigo', 'Galicia'), ('4', 'Dolores', 'Barriga', '54878289v', 'C/ fueres 25', 'Madrid', 'Madrid'), ('5', 'Juan', 'Gonzalez', '25484524v', 'C/ federico 54', 'Madrid', 'Madrid'), ('6', 'Carmen', 'Gaarcia', '87458745r', 'C/ miraflores 89', 'Sevilla', 'Andalucia'), ('7', 'Sergio', 'Moebius', '45875128d', 'C/ Rosario Pino 33', 'Madrid', 'Madrid'), ('8', 'Maria', 'Mellas', '87822453v', 'C/ miricias s/n', 'Madrid', 'Madrid'), ('9', 'Sofia', 'Metetodo', '69558457d', 'C/ La pille 69', 'Madrid', 'Madrid'), ('10', 'Ignacio', 'Oleole', '58425445d', 'C/viva españa', 'Madrid', 'Madrid'); COMMIT; -- ---------------------------- -- Table structure for `taller` -- ---------------------------- DROP TABLE IF EXISTS `taller`; CREATE TABLE `taller` ( `id_reparacion` int(11) unsigned NOT NULL AUTO_INCREMENT, `matricula` varchar(8) DEFAULT NULL, `intervencion` varchar(50) DEFAULT NULL, `importe` float DEFAULT NULL, `fecha` date DEFAULT NULL, PRIMARY KEY (`id_reparacion`) ) ENGINE=InnoDB AUTO_INCREMENT=17 DEFAULT CHARSET=latin1; -- ---------------------------- -- Records of `taller` -- ---------------------------- BEGIN; INSERT INTO `taller` VALUES ('1', '0001AAA', 'Cambio de aceite', '500', '2010-05-05'), ('2', '0002AAA', 'SUSPENSIONES', '250', '2009-06-04'), ('3', '0003AAA', 'FRENOS', '150', '2009-02-06'), ('4', '0004AAA', 'LIQUIDOS', '250', '2010-02-03'), ('5', '0005AAA', 'PINTURA', '600', '2011-03-08'), ('6', '0006AAA', 'PINTURA', '300', '2010-03-04'), ('7', '0007AAA', 'DIRECCION', '140', '2010-03-05'), ('8', '0008AAA', 'FAROS', '212', '2010-06-09'), ('9', '0009AAA', 'CHAPA', '400', '2010-04-07'), ('10', '0010AAA', 'PINTURA', '300', '2010-05-03'), ('11', '0001AAA', 'RUEDAS', '210', '2010-06-09'), ('12', '0002AAA', 'RUEDAS', '250', '2010-06-09'), ('13', '0003AAA', 'RUEDAS', '240', '2010-03-07'), ('14', '0004AAA', 'RUEDAS', '250', '2010-05-03'), ('15', '0005AAA', 'FRENOS', '240', '2010-02-02'), ('16', '0006AAA', 'TAPICERIA', '540', '2010-02-01'); COMMIT; -- ---------------------------- -- Table structure for `transaccion` -- ---------------------------- DROP TABLE IF EXISTS `transaccion`; CREATE TABLE `transaccion` ( `id_transaccion` int(11) unsigned NOT NULL AUTO_INCREMENT, `numCliente` int(11) DEFAULT NULL, `operacion` varchar(6) DEFAULT NULL, `matricula` varchar(8) DEFAULT NULL, `importe` float DEFAULT NULL, `fecha` date DEFAULT NULL, PRIMARY KEY (`id_transaccion`) ) ENGINE=InnoDB AUTO_INCREMENT=18 DEFAULT CHARSET=latin1; -- ---------------------------- -- Records of `transaccion` -- ---------------------------- BEGIN; INSERT INTO `transaccion` VALUES ('1', '1', 'VENTA', '0001AAA', '10000', '2003-12-02'), ('2', '2', 'VENTA', '0002AAA', '3000', '2002-02-15'), ('3', '3', 'VENTA', '0003AAA', '5000', '2010-02-05'), ('4', '4', 'VENTA', '0004AAA', '3500', '2009-06-15'), ('5', '5', 'VENTA', '0005AAA', '2500', '2000-06-06'), ('6', '6', 'VENTA', '0006AA', '6500', '1999-05-05'), ('7', '7', 'VENTA', '0007AAA', '4000', '2000-11-22'), ('8', '8', 'VENTA', '0008AAA', '2500', '1998-07-09'), ('9', '9', 'VENTA', '0009AAA', '1000', '2007-02-03'), ('10', '10', 'VENTA', '0010AAA', '3650', '2005-03-06'), ('11', '1', 'COMPRA', '0010AAA', '5000', '2006-02-02'), ('12', '2', 'COMPRA', '0009AAA', '2500', '2008-03-25'), ('13', '3', 'COMPRA', '0008AAA', '5000', '2000-05-06'), ('14', '4', 'COMPRA', '0007AAA', '6500', '2011-06-06'), ('15', '5', 'COMPRA', '0004AAA', '8000', '2011-05-05'), ('16', '6', 'COMPRA', '0005AAA', '5000', '2011-06-02'), ('17', '7', 'COMPRA', '0001AAA', '15000', '2011-05-06'); COMMIT; SET FOREIGN_KEY_CHECKS = 1; |
Esquema de E/R

Realizar las siguientes consultas en la BD.
- Mostrar todos los vehículos de la BD
- Mostrar una lista de los vehiculos con los kilometros de su ultima revisión (Usar inner Join)
- Calcular la suma de los importes de todos los vehículos vendidos.
- Calcular los beneficios o perdidas de la venta/compra de los vehiculos.
- Mostrar las transaciones realizadas entre los imprtes de 2000 y 10000
- Mostrar las transaciones realizadas entre los imprtes que no sean de 2000 y 10000
- Si el coche tiene que pasar la revision cada 2 años, Mostrar los que superen ese tiempo.
- Calcular el coste total, precio compra y costes de taller de un vehículo determinado. Y de un solo Coche
- Cuando hizo por ultima vez la itv Juan Gonzalez.
Solución
1. Mostrar todos los vehículos de la BBDD
|
1 2 |
SELECT * FROM coche |
2. Mostrar una lista de los vehiculos con los kilometros de su ultima revisión (Usar inner Join)
|
1 2 |
SELECT coche.matricula, coche.modelo, mantenimiento.kilometros FROM mantenimiento inner join coche on coche.id = mantenimiento.id |
3. Calcular la suma de los importes de todos los vehículos vendidos.
|
1 2 3 |
SELECT SUM(importe) FROM transaccion WHERE operacion = ‘venta’ |
4. Calcular los beneficios o perdidas de la venta/compra de los vehiculos.
|
1 2 3 4 5 |
SELECT (SELECT SUM(importe) FROM transaccion WHERE operacion = ‘venta’) - (SELECT SUM(importe) FROM transaccion WHERE operacion = ‘compra’) Beneficios |
5. Mostrar las transaciones realizadas entre los imprtes de 2000 y 10000
|
1 2 3 |
SELECT * FROM transaccion where importe BETWEEN 2000 AND 10000 |
6. Mostrar las transaciones realizadas entre los importes que no sean de 2000 y 10000
|
1 2 3 |
SELECT * FROM transaccion where importe NOT BETWEEN 2000 AND 10000 |
7. Si el coche tiene que pasar la revision cada 2 años, Mostrar los que superen ese tiempo.
|
1 2 3 |
SELECT DISTINCT modelo, marca, coche.matricula FROM mantenimiento INNER JOIN coche ON mantenimiento.matricula = coche.matricula WHERE YEAR(mantenimiento.itv) < YEAR(CURDATE()) - 2 |
8. Calcular el coste total, precio compra y costes de taller de un vehículo determinado.
|
1 2 3 4 5 |
SELECT DISTINCT coche.matricula,(transaccion.importe + taller.importe) # Como se puede apreciar, aqui tenemos un error de diseño, pues las actuaciones en cada coche deberian tener una tabla aparte FROM transaccion INNER JOIN coche ON transaccion.matricula = coche.matricula INNER JOIN taller ON taller.matricula = coche.matricula where operacion = ‘compra’ |
Y de un solo Coche
|
1 2 3 4 5 |
SELECT coche.matricula, (transaccion.importe + SUM(taller.importe)) # Aunque funcina seguimos arrastrando el error de diseño FROM transaccion INNER JOIN coche ON transaccion.matricula = coche.matricula INNER JOIN taller ON taller.matricula = coche.matricula where operacion = ‘compra’ AND coche.matricula = ‘0001AAA’ |
9. Cuando hizo por ultima vez la itv Juan Gonzalez
|
1 2 3 4 5 |
SELECT nombre, apellidos, coche.matricula, itv FROM personas INNER JOIN transaccion ON personas.id_nCliente = transaccion.numCliente INNER JOIN coche ON transaccion.matricula = coche.matricula INNER JOIN mantenimiento ON coche.matricula = mantenimiento.matricula WHERE nombre = ‘Juan’ AND apellidos = ‘Gonzalez’ AND transaccion.operacion = ‘compra’ |