Tema: Semana 53
Ver Mensaje Individual
  #6 (permalink)  
Antiguo 07/11/2017, 11:55
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: Semana 53

Ya mas o menos entendi que es lo que quieres hacer, ahorita tienes el problema con la semana 9, la siguiente semana lo vas a tener con la 8 y la 9 y asi sucesivamente, no seria mas facil sacar un datediff de semanas?

Código SQL:
Ver original
  1. SELECT
  2. CASE
  3. WHEN datediff(wk,getdate(), c.FechaProbablePago)=1
  4. THEN
  5. c.AbonoCuentaProveedor - isNull ((SELECT SUM(Monto) FROM medacasa18.dbo.CtbPolizasDetAP x WHERE x.IdPolizaDetAbono =  b.IdPolizaDet),0)
  6. END SEM1,
  7. CASE
  8. WHEN datediff(wk,getdate(), c.FechaProbablePago)=2
  9. THEN
  10. c.AbonoCuentaProveedor - isNull ((SELECT SUM(Monto) FROM medacasa18.dbo.CtbPolizasDetAP x WHERE x.IdPolizaDetAbono =  b.IdPolizaDet),0)
  11. END SEM2



Código SQL:
Ver original
  1. CREATE TABLE #fechas(
  2. fecha datetime
  3. )
  4.  
  5. INSERT INTO #fechas VALUES (dateadd(wk,1,getdate()))
  6. INSERT INTO #fechas VALUES (dateadd(wk,2,getdate()))
  7. INSERT INTO #fechas VALUES (dateadd(wk,3,getdate()))
  8. INSERT INTO #fechas VALUES (dateadd(wk,4,getdate()))
  9. INSERT INTO #fechas VALUES (dateadd(wk,5,getdate()))
  10. INSERT INTO #fechas VALUES (dateadd(wk,6,getdate()))
  11. INSERT INTO #fechas VALUES (dateadd(wk,7,getdate()))
  12. INSERT INTO #fechas VALUES (dateadd(wk,8,getdate()))
  13. INSERT INTO #fechas VALUES (dateadd(wk,9,getdate()))
  14. INSERT INTO #fechas VALUES (dateadd(wk,10,getdate()))
  15. INSERT INTO #fechas VALUES (dateadd(wk,11,getdate()))
  16.  
  17.  
  18.  
  19.  
  20. SELECT datediff(wk,getdate(), fecha) AS wk,
  21. fecha FROM #fechas



wk fecha
1 2017-11-14 14:06:05.087
2 2017-11-21 14:06:05.100
3 2017-11-28 14:06:05.100
4 2017-12-05 14:06:05.100
5 2017-12-12 14:06:05.100
6 2017-12-19 14:06:05.100
7 2017-12-26 14:06:05.100
8 2018-01-02 14:06:05.100
9 2018-01-09 14:06:05.100
10 2018-01-16 14:06:05.100
11 2018-01-23 14:06:05.100
__________________
What does an execution plan say to t-sql query? Go f**k yourself, if you are not happy with me