CREATE TABLE clientes(
id INT IDENTITY(1,1),
nombre Nvarchar(20)
)
INSERT INTO clientes VALUES ('Libras')
INSERT INTO clientes VALUES ('Libras1')
INSERT INTO clientes VALUES ('Libras2')
CREATE TABLE cuentas(
id INT IDENTITY(1,1),
id_cliente INT,
monto INT
)
INSERT INTO cuentas VALUES (1,50)
INSERT INTO cuentas VALUES (2,100)
INSERT INTO cuentas VALUES (3,50)
CREATE TABLE meses(
id INT IDENTITY(1,1),
mes Nvarchar(20)
)
INSERT INTO meses VALUES ('Enero')
INSERT INTO meses VALUES ('Febrero')
INSERT INTO meses VALUES ('Marzo')
INSERT INTO meses VALUES ('Abril')
INSERT INTO meses VALUES ('Mayo')
INSERT INTO meses VALUES ('junio')
INSERT INTO meses VALUES ('julio')
INSERT INTO meses VALUES ('Agosto')
INSERT INTO meses VALUES ('Septiembre')
INSERT INTO meses VALUES ('Octubre')
INSERT INTO meses VALUES ('noviembre')
INSERT INTO meses VALUES ('diciembre')
CREATE TABLE years(
id INT IDENTITY(1,1),
years INT
)
INSERT INTO years VALUES (2016)
INSERT INTO years VALUES (2017)
CREATE TABLE years_months(
id INT IDENTITY(1,1),
id_year INT,
id_mont INT
)
INSERT INTO years_months VALUES (1,1)
INSERT INTO years_months VALUES (1,2)
INSERT INTO years_months VALUES (1,3)
INSERT INTO years_months VALUES (1,4)
INSERT INTO years_months VALUES (1,5)
INSERT INTO years_months VALUES (1,6)
INSERT INTO years_months VALUES (1,7)
INSERT INTO years_months VALUES (1,8)
INSERT INTO years_months VALUES (1,9)
INSERT INTO years_months VALUES (1,10)
INSERT INTO years_months VALUES (1,11)
INSERT INTO years_months VALUES (1,12)
CREATE TABLE pagos(
id INT IDENTITY(1,1),
id_cliente INT,
id_year_month INT,
cantidad INT
)
INSERT INTO pagos VALUES (1,1,50)
INSERT INTO pagos VALUES (2,1,100)
INSERT INTO pagos VALUES (1,2,100)
------------------------------------
--los que han pagado en el mes
SELECT t1.id, t1.nombre FROM clientes AS t1
LEFT JOIN pagos AS t2 ON (t1.id=t2.id_cliente)
LEFT JOIN years_months AS t3 ON (t2.id_year_month=t3.id)
LEFT JOIN meses AS t4 ON (t3.id_mont=t4.id)
LEFT JOIN years AS t5 ON (t3.id_year=t5.id)
WHERE t4.mes='febrero' AND t5.years=2016
-------------------------------------
SELECT t1.* FROM clientes AS t1
LEFT JOIN (
SELECT t1.id, t1.nombre FROM clientes AS t1
LEFT JOIN pagos AS t2 ON (t1.id=t2.id_cliente)
LEFT JOIN years_months AS t3 ON (t2.id_year_month=t3.id)
LEFT JOIN meses AS t4 ON (t3.id_mont=t4.id)
LEFT JOIN years AS t5 ON (t3.id_year=t5.id)
WHERE t4.mes='enero' AND t5.years=2016) AS pagados ON (t1.id=pagados.id AND t1.nombre=pagados.nombre)
WHERE pagados.nombre IS NULL
--relacion de pagos contra lo que tiene que pagar segun su cuenta
SELECT t1.nombre,
t2.cantidad,
t6.monto,
t6.monto-t2.cantidad AS faltante
FROM clientes AS t1
LEFT JOIN pagos AS t2 ON (t1.id=t2.id_cliente)
LEFT JOIN years_months AS t3 ON (t2.id_year_month=t3.id)
LEFT JOIN meses AS t4 ON (t3.id_mont=t4.id)
LEFT JOIN years AS t5 ON (t3.id_year=t5.id)
LEFT JOIN cuentas AS t6 ON (t1.id=t6.id_cliente)
WHERE t4.mes='febrero' AND t5.years=2016
UNION
SELECT t1.nombre,0, t2.monto, t2.monto AS faltante FROM clientes AS t1
LEFT JOIN (
SELECT t1.id, t1.nombre FROM clientes AS t1
LEFT JOIN pagos AS t2 ON (t1.id=t2.id_cliente)
LEFT JOIN years_months AS t3 ON (t2.id_year_month=t3.id)
LEFT JOIN meses AS t4 ON (t3.id_mont=t4.id)
LEFT JOIN years AS t5 ON (t3.id_year=t5.id)
WHERE t4.mes='febrero' AND t5.years=2016) AS pagados ON (t1.id=pagados.id AND t1.nombre=pagados.nombre)
LEFT JOIN cuentas AS t2 ON (t1.id=t2.id_cliente)
WHERE pagados.nombre IS NULL