Ver Mensaje Individual
  #2 (permalink)  
Antiguo 20/01/2014, 11:52
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, 5 meses
Puntos: 774
Respuesta: Como seleccionar el campo con la fecha mas reciente de este select?

Código SQL:
Ver original
  1. SELECT * FROM(
  2. SELECT ROW_NUMBER()OVER (partition BY lastdate ORDER BY lastdate ASC) AS rn, * FROM
  3. (
  4. SELECT MAX(H.[SettledDate]) AS 'LastDate', J.[Player], CONVERT(VARCHAR(8), G.[GradedDate] , 108) AS 'Time', D.[RESULT], H.[RiskAmount], H.[WinAmount], G.[VisitorTeam] + ' @ ' +G.[HomeTeam] AS 'Teams',
  5.        P.[PeriodDescription], CONVERT(VARCHAR(MAX),G.[VisitorNumber]) + '/' + CONVERT(VARCHAR(MAX),G.[HomeNumber]) AS 'NumGame', D.[IdWager], W.[Description]  
  6. FROM GRADEDWAGERHEADER H, GRADEDWAGERDETAIL D, GRADEDGAME G, PERIOD P, AGENT A, WAGERTYPE W, PLAYER J
  7. WHERE H.[IdWager]  = D.[IdWager] AND
  8.       D.[IdGame]   = G.[IdGame]  AND
  9.       H.[IdAgent]  = A.[IdAgent] AND
  10.       H.[IdPlayer] = J.[IdPlayer] AND
  11.       H.[IdWagerType] = W.[IdWagerType] AND
  12.       D.[IdSport] = P.[IdSport] AND
  13.       D.[Period]  = P.[NumberOfPeriod] AND
  14.       H.[IdAgent] = 5585 AND
  15.       D.[IdWager] = 12683461 AND
  16.       D.[RESULT]  = 1
  17. GROUP BY J.[Player], G.[GradedDate], D.[RESULT], H.[RiskAmount], H.[WinAmount], G.[VisitorTeam], G.[HomeTeam],
  18.          P.[PeriodDescription], G.[VisitorNumber], G.[HomeNumber], D.[IdWager], W.[Description]
  19. ORDER BY TIME DESC
  20. ) AS t1
  21. ) t2 WHERE rn=1
__________________
What does an execution plan say to t-sql query? Go f**k yourself, if you are not happy with me