我是新来的sql目前正在使用一本书来学习它我必须使用表格
CREATE TABLE MOV (
codigo NUMBER(5),
tipo char(1),
monto NUMBER(7, 2));
INSERT INTO MOV VALUES (1,'I',5000.00);
INSERT INTO MOV VALUES (2,'I',6000.00);
INSERT INTO MOV VALUES (3,'I',7000.00);
INSERT INTO MOV VALUES (4,'I',8000.00);
INSERT INTO MOV VALUES (5,'I',9000.00);
INSERT INTO MOV VALUES (6,'I',9500.00);
INSERT INTO MOV VALUES (7,'I',8500.00);
INSERT INTO MOV VALUES (8,'I',7500.00);
INSERT INTO MOV VALUES (9,'I',6500.00);
INSERT INTO MOV VALUES (10,'I',5500.00);
INSERT INTO MOV VALUES (1,'I',5650.00);
INSERT INTO MOV VALUES (2,'I',6650.00);
INSERT INTO MOV VALUES (3,'I',7650.00);
INSERT INTO MOV VALUES (4,'I',8650.00);
INSERT INTO MOV VALUES (5,'I',9650.00);
INSERT INTO MOV VALUES (6,'I',10150.00);
INSERT INTO MOV VALUES (7,'I',9150.00);
INSERT INTO MOV VALUES (8,'I',8150.00);
INSERT INTO MOV VALUES (9,'I',7150.00);
INSERT INTO MOV VALUES (10,'I',6150.00);
INSERT INTO MOV VALUES (1,'I',6300.00);
INSERT INTO MOV VALUES (2,'I',7300.00);
INSERT INTO MOV VALUES (3,'I',8300.00);
INSERT INTO MOV VALUES (4,'I',9300.00);
INSERT INTO MOV VALUES (5,'I',10300.00);
INSERT INTO MOV VALUES (6,'I',10800.00);
INSERT INTO MOV VALUES (7,'I',9800.00);
INSERT INTO MOV VALUES (8,'I',8800.00);
INSERT INTO MOV VALUES (9,'I',7800.00);
INSERT INTO MOV VALUES (10,'I',6800.00);
INSERT INTO MOV VALUES (1,'I',6950.00);
INSERT INTO MOV VALUES (2,'I',7950.00);
INSERT INTO MOV VALUES (3,'I',8950.00);
INSERT INTO MOV VALUES (4,'I',9950.00);
INSERT INTO MOV VALUES (5,'I',10950.00);
INSERT INTO MOV VALUES (6,'I',11450.00);
INSERT INTO MOV VALUES (7,'I',10450.00);
INSERT INTO MOV VALUES (8,'I',9450.00);
INSERT INTO MOV VALUES (9,'I',8450.00);
INSERT INTO MOV VALUES (10,'I',7450.00);
INSERT INTO MOV VALUES (1,'I',7600.00);
INSERT INTO MOV VALUES (2,'I',8600.00);
INSERT INTO MOV VALUES (3,'I',9600.00);
INSERT INTO MOV VALUES (4,'I',10600.00);
INSERT INTO MOV VALUES (5,'I',11600.00);
INSERT INTO MOV VALUES (6,'I',12100.00);
INSERT INTO MOV VALUES (7,'I',11100.00);
INSERT INTO MOV VALUES (8,'I',10100.00);
INSERT INTO MOV VALUES (9,'I',9100.00);
INSERT INTO MOV VALUES (10,'I',8100.00);
INSERT INTO MOV VALUES (1,'D',3800.00);
INSERT INTO MOV VALUES (2,'D',4300.00);
INSERT INTO MOV VALUES (3,'D',4800.00);
INSERT INTO MOV VALUES (4,'D',5300.00);
INSERT INTO MOV VALUES (5,'D',5800.00);
INSERT INTO MOV VALUES (6,'D',6050.00);
INSERT INTO MOV VALUES (7,'D',5550.00);
INSERT INTO MOV VALUES (8,'D',5050.00);
INSERT INTO MOV VALUES (9,'D',4550.00);
INSERT INTO MOV VALUES (10,'D',4050.00);
INSERT INTO MOV VALUES (1,'D',1900.00);
INSERT INTO MOV VALUES (2,'D',2150.00);
INSERT INTO MOV VALUES (3,'D',2400.00);
INSERT INTO MOV VALUES (4,'D',2650.00);
INSERT INTO MOV VALUES (5,'D',2900.00);
INSERT INTO MOV VALUES (6,'D',3025.00);
INSERT INTO MOV VALUES (7,'D',2775.00);
INSERT INTO MOV VALUES (8,'D',2525.00);
INSERT INTO MOV VALUES (9,'D',2275.00);
INSERT INTO MOV VALUES (10,'D',2025.00);
INSERT INTO MOV VALUES (1,'D',950.00);
INSERT INTO MOV VALUES (2,'D',1075.00);
INSERT INTO MOV VALUES (3,'D',1200.00);
INSERT INTO MOV VALUES (4,'D',1325.00);
INSERT INTO MOV VALUES (5,'D',1450.00);
INSERT INTO MOV VALUES (6,'D',1512.50);
INSERT INTO MOV VALUES (7,'D',1387.50);
INSERT INTO MOV VALUES (8,'D',1262.50);
INSERT INTO MOV VALUES (9,'D',1137.50);
INSERT INTO MOV VALUES (10,'D',1012.50);
INSERT INTO MOV VALUES (1,'I',2600.00);
INSERT INTO MOV VALUES (2,'I',2725.00);
INSERT INTO MOV VALUES (3,'I',2850.00);
INSERT INTO MOV VALUES (4,'I',2975.00);
INSERT INTO MOV VALUES (5,'I',3100.00);
INSERT INTO MOV VALUES (6,'I',3162.50);
INSERT INTO MOV VALUES (7,'I',3037.50);
INSERT INTO MOV VALUES (8,'I',2912.50);
INSERT INTO MOV VALUES (9,'I',2787.50);
INSERT INTO MOV VALUES (10,'I',2662.50);
INSERT INTO MOV VALUES (1,'D',650.00);
INSERT INTO MOV VALUES (2,'D',681.25);
INSERT INTO MOV VALUES (3,'D',712.50);
INSERT INTO MOV VALUES (4,'D',743.75);
INSERT INTO MOV VALUES (5,'D',775.00);
INSERT INTO MOV VALUES (6,'D',790.63);
INSERT INTO MOV VALUES (7,'D',759.38);
INSERT INTO MOV VALUES (8,'D',728.13);
INSERT INTO MOV VALUES (9,'D',696.88);
INSERT INTO MOV VALUES (10,'D',475.25);
CREATE TABLE EMPLE (
codigo NUMBER(5),
nombre char(40));
INSERT INTO EMPLE VALUES (1,'PEDRO PEREZ MORA');
INSERT INTO EMPLE VALUES (2,'MARIA BRENES ORTIZ');
INSERT INTO EMPLE VALUES (3,'CARLOS QUESADA BONILLA');
INSERT INTO EMPLE VALUES (4,'LUISA SOTO ABARCA');
INSERT INTO EMPLE VALUES (5,'OLMAN BADILLA CORTES');
INSERT INTO EMPLE VALUES (6,'ANA CAMACHO DIAZ');
INSERT INTO EMPLE VALUES (7,'ALVARO DUARTE ESPINOZA');
INSERT INTO EMPLE VALUES (8,'HAZEL ELIZONDO GAMBOA');
INSERT INTO EMPLE VALUES (9,'HUGO GARITA JIMENEZ');
INSERT INTO EMPLE VALUES (10,'LIDIETH JARA ECHANDI');
INSERT INTO EMPLE VALUES (11,'JOSE MORA QUIROS');
INSERT INTO EMPLE VALUES (12,'ROY PAZOS RODRIGUEZ');
最终结果应该是这样的:
我所拥有的是一个简单的选择陈述
select e.codigo, e.nombre
from mov e;
如何使查询在图像中具有预期结果 感谢
答案 0 :(得分:1)
使用GROUP BY的简单LEFT JOIN应该这样做;
SELECT e.codigo, e.nombre,
SUM(CASE WHEN m.tipo = 'I' THEN m.monto ELSE 0 END) ingresos,
SUM(CASE WHEN m.tipo = 'D' THEN m.monto ELSE 0 END) deducciones,
SUM(CASE WHEN m.tipo = 'I' THEN m.monto ELSE 0 END) -
SUM(CASE WHEN m.tipo = 'D' THEN m.monto ELSE 0 END) neto
FROM emple e
LEFT JOIN mov m
ON e.codigo = m.codigo
GROUP BY e.codigo, e.nombre
ORDER BY e.codigo, e.nombre
LEFT JOIN让我们为每个“emple”收集所有相应的“mov”,GROUP BY允许我们为每个codigo / nombre组合总结它们。
答案 1 :(得分:0)
如果我理解,你需要加入这两个表。
像...一样的东西。
Select e.*,m.* from emple e,mov m where e.codigo = m.codigo;