Ver Mensaje Individual
  #19 (permalink)  
Antiguo 10/02/2016, 12:34
Avatar de Libras
Libras
Colaborador
 
Fecha de Ingreso: agosto-2006
Ubicación: En la hermosa perla de occidente
Mensajes: 7.412
Antigüedad: 18 años, 4 meses
Puntos: 774
Respuesta: extraer solo los usuarios que no hayan cancelado un mes

un pequeño cambio:

Código SQL:
Ver original
  1. IF OBJECT_ID('dbo.years_months', 'U') IS NOT NULL
  2.   DROP TABLE dbo.years_months;
  3.  
  4. IF OBJECT_ID('dbo.pagos', 'U') IS NOT NULL
  5.   DROP TABLE dbo.pagos;
  6.  
  7. IF OBJECT_ID('dbo.cuentas', 'U') IS NOT NULL
  8.   DROP TABLE dbo.cuentas;
  9.  
  10.  
  11. IF OBJECT_ID('dbo.clientes', 'U') IS NOT NULL
  12.   DROP TABLE dbo.clientes;
  13.    
  14.  
  15. IF OBJECT_ID('dbo.meses', 'U') IS NOT NULL
  16.   DROP TABLE dbo.meses;
  17.  
  18. IF OBJECT_ID('dbo.years', 'U') IS NOT NULL
  19.   DROP TABLE dbo.years;
  20.  
  21.  
  22.  
  23.  
  24. CREATE TABLE clientes(
  25. id INT IDENTITY(1,1),
  26. nombre Nvarchar(20)
  27. )
  28.  
  29. INSERT INTO clientes VALUES ('Libras')
  30. INSERT INTO clientes VALUES ('Libras1')
  31. INSERT INTO clientes VALUES ('Libras2')
  32.  
  33.  
  34. CREATE TABLE cuentas(
  35. id INT IDENTITY(1,1),
  36. id_cliente INT,
  37. years_months INT,
  38. monto INT
  39. )
  40.  
  41. INSERT INTO cuentas VALUES (1,1,50)
  42. INSERT INTO cuentas VALUES (1,2,100)
  43. INSERT INTO cuentas VALUES (1,3,50)
  44. INSERT INTO cuentas VALUES (1,4,50)
  45. INSERT INTO cuentas VALUES (1,5,100)
  46. INSERT INTO cuentas VALUES (1,6,50)
  47. INSERT INTO cuentas VALUES (1,7,50)
  48. INSERT INTO cuentas VALUES (1,8,100)
  49. INSERT INTO cuentas VALUES (1,9,50)
  50. INSERT INTO cuentas VALUES (1,10,50)
  51. INSERT INTO cuentas VALUES (1,11,100)
  52. INSERT INTO cuentas VALUES (1,12,50)
  53.  
  54.  
  55. INSERT INTO cuentas VALUES (2,1,50)
  56. INSERT INTO cuentas VALUES (2,2,100)
  57. INSERT INTO cuentas VALUES (2,3,50)
  58. INSERT INTO cuentas VALUES (2,4,50)
  59. INSERT INTO cuentas VALUES (2,5,100)
  60. INSERT INTO cuentas VALUES (2,6,50)
  61. INSERT INTO cuentas VALUES (2,7,50)
  62. INSERT INTO cuentas VALUES (2,8,100)
  63. INSERT INTO cuentas VALUES (2,9,50)
  64. INSERT INTO cuentas VALUES (2,10,50)
  65. INSERT INTO cuentas VALUES (2,11,100)
  66. INSERT INTO cuentas VALUES (2,12,50)
  67.  
  68.  
  69. INSERT INTO cuentas VALUES (3,1,50)
  70. INSERT INTO cuentas VALUES (3,2,100)
  71. INSERT INTO cuentas VALUES (3,3,50)
  72. INSERT INTO cuentas VALUES (3,4,50)
  73. INSERT INTO cuentas VALUES (3,5,100)
  74. INSERT INTO cuentas VALUES (3,6,50)
  75. INSERT INTO cuentas VALUES (3,7,50)
  76. INSERT INTO cuentas VALUES (3,8,100)
  77. INSERT INTO cuentas VALUES (3,9,50)
  78. INSERT INTO cuentas VALUES (3,10,50)
  79. INSERT INTO cuentas VALUES (3,11,100)
  80. INSERT INTO cuentas VALUES (3,12,50)
  81.  
  82.  
  83. CREATE TABLE meses(
  84. id INT IDENTITY(1,1),
  85. mes Nvarchar(20)
  86. )
  87.  
  88. INSERT INTO meses VALUES ('Enero')
  89. INSERT INTO meses VALUES ('Febrero')
  90. INSERT INTO meses VALUES ('Marzo')
  91. INSERT INTO meses VALUES ('Abril')
  92. INSERT INTO meses VALUES ('Mayo')
  93. INSERT INTO meses VALUES ('junio')
  94. INSERT INTO meses VALUES ('julio')
  95. INSERT INTO meses VALUES ('Agosto')
  96. INSERT INTO meses VALUES ('Septiembre')
  97. INSERT INTO meses VALUES ('Octubre')
  98. INSERT INTO meses VALUES ('noviembre')
  99. INSERT INTO meses VALUES ('diciembre')
  100.  
  101. CREATE TABLE years(
  102. id INT IDENTITY(1,1),
  103. years INT
  104. )
  105.  
  106. INSERT INTO years VALUES (2016)
  107. INSERT INTO years VALUES (2017)
  108.  
  109. CREATE TABLE years_months(
  110. id INT IDENTITY(1,1),
  111. id_year INT,
  112. id_mont INT
  113. )
  114.  
  115.  
  116. INSERT INTO years_months VALUES (1,1)
  117. INSERT INTO years_months VALUES (1,2)
  118. INSERT INTO years_months VALUES (1,3)
  119. INSERT INTO years_months VALUES (1,4)
  120. INSERT INTO years_months VALUES (1,5)
  121. INSERT INTO years_months VALUES (1,6)
  122. INSERT INTO years_months VALUES (1,7)
  123. INSERT INTO years_months VALUES (1,8)
  124. INSERT INTO years_months VALUES (1,9)
  125. INSERT INTO years_months VALUES (1,10)
  126. INSERT INTO years_months VALUES (1,11)
  127. INSERT INTO years_months VALUES (1,12)
  128.  
  129.  
  130. CREATE TABLE pagos(
  131. id INT IDENTITY(1,1),
  132. id_cliente INT,
  133. id_year_month INT,
  134. cantidad INT
  135. )
  136.  
  137.  
  138.  
  139.  
  140. INSERT INTO pagos VALUES (1,1,50)
  141. INSERT INTO pagos VALUES (2,1,100)
  142. INSERT INTO pagos VALUES (1,2,100)
  143.  
  144. ------------------------------------
  145.  
  146. SELECT * FROM(
  147. SELECT nombre,mes, total_mes, pagado_mes, total_mes - pagado_mes AS faltante FROM(
  148. SELECT t3.nombre,t5.mes, t1.monto total_mes ,isnull(t2.cantidad,0) pagado_mes  FROM cuentas AS t1
  149. LEFT JOIN pagos AS t2 ON (t1.years_months=t2.id_year_month AND t2.id_cliente=t1.id_cliente)
  150. LEFT JOIN clientes AS t3 ON (t1.id_cliente=t3.id)
  151. LEFT JOIN years_months AS t4 ON (t1.years_months=t4.id)
  152. LEFT JOIN meses AS t5 ON (t4.id_mont=t5.id)
  153. ) AS parcial
  154. ) AS completa WHERE faltante>=total_mes
  155.  
  156. ----------------------
  157.  
  158. SELECT * FROM(
  159. SELECT nombre,mes, total_mes, pagado_mes, total_mes - pagado_mes AS faltante, years FROM(
  160. SELECT t3.nombre,t5.mes, t1.monto total_mes ,isnull(t2.cantidad,0) pagado_mes, t6.years  FROM cuentas AS t1
  161. LEFT JOIN pagos AS t2 ON (t1.years_months=t2.id_year_month AND t2.id_cliente=t1.id_cliente)
  162. LEFT JOIN clientes AS t3 ON (t1.id_cliente=t3.id)
  163. LEFT JOIN years_months AS t4 ON (t1.years_months=t4.id)
  164. LEFT JOIN meses AS t5 ON (t4.id_mont=t5.id)
  165. LEFT JOIN years AS t6 ON (t4.id_year=t6.id)
  166. ) AS parcial
  167. ) AS completa WHERE faltante<=0 AND mes='Enero' AND years=2016
  168.  
  169. ------------------------------
  170.  
  171. SELECT * FROM(
  172. SELECT nombre,mes, total_mes, pagado_mes, total_mes - pagado_mes AS faltante, years FROM(
  173. SELECT t3.nombre,t5.mes, t1.monto total_mes ,isnull(t2.cantidad,0) pagado_mes, t6.years  FROM cuentas AS t1
  174. LEFT JOIN pagos AS t2 ON (t1.years_months=t2.id_year_month AND t2.id_cliente=t1.id_cliente)
  175. LEFT JOIN clientes AS t3 ON (t1.id_cliente=t3.id)
  176. LEFT JOIN years_months AS t4 ON (t1.years_months=t4.id)
  177. LEFT JOIN meses AS t5 ON (t4.id_mont=t5.id)
  178. LEFT JOIN years AS t6 ON (t4.id_year=t6.id)
  179. ) AS parcial
  180. ) AS completa WHERE faltante>=total_mes AND mes='Enero' AND years=2016
__________________
What does an execution plan say to t-sql query? Go f**k yourself, if you are not happy with me