Ver Mensaje Individual
  #4 (permalink)  
Antiguo 21/10/2013, 11:10
Avatar de Libras
Libras
Colaborador
 
Fecha de Ingreso: agosto-2006
Ubicación: En la hermosa perla de occidente
Mensajes: 7.412
Antigüedad: 17 años, 8 meses
Puntos: 774
Respuesta: Diferencias entre dos fechas dif columna

utilizando DATEDIFF, y claro usando subqueries ;)

Código SQL:
Ver original
  1. CREATE TABLE #temp
  2. (
  3. dato VARCHAR(20),
  4. dato2 datetime,
  5. ROW INT
  6. )
  7. INSERT INTO #temp VALUES ('MOR-BK005','2011-08-26 00:00:00.000',1)
  8. INSERT INTO #temp VALUES ('MOR-BK005','2012-02-14 00:00:00.000' ,2)
  9. INSERT INTO #temp VALUES ('MOR-BK005','2012-06-28 00:00:00.000' ,3)
  10. INSERT INTO #temp VALUES ('MOR-BK005','2012-10-11 00:00:00.000' ,4)
  11. INSERT INTO #temp VALUES ('MOR-BK005','2013-01-15 00:00:00.000' ,5)
  12. INSERT INTO #temp VALUES ('MOR-BK005','2013-05-13 00:00:00.000' ,6)
  13. INSERT INTO #temp VALUES ('MOR-BK005','2013-08-10 00:00:00.000',7)
  14.  
  15.  
  16. SELECT t1.dato,t1.dato2,t2.dato2,datediff(mm,t1.dato2,t2.dato2) AS dif_mes FROM #temp AS t1
  17. LEFT JOIN #temp AS t2 ON (t2.ROW=t1.ROW+1)
  18. WHERE t2.dato IS NOT NULL

saludos!
__________________
What does an execution plan say to t-sql query? Go f**k yourself, if you are not happy with me