Foros del Web » Programación para mayores de 30 ;) » Bases de Datos General » SQL Server »

Query Que Me Devuelva La Ultima Fecha

Estas en el tema de Query Que Me Devuelva La Ultima Fecha en el foro de SQL Server en Foros del Web. HOLA, NECESITO HACER UNA CONSULTA EN UNA TABLA EN DONDE SE GUARDAN TODOS LOS REGISTROS DE LOS ARTICULOS, BASICAMENTE LA INFORMACION QUE NECESITO ES ULTIMA ...
  #1 (permalink)  
Antiguo 07/10/2007, 01:03
 
Fecha de Ingreso: marzo-2007
Mensajes: 76
Antigüedad: 17 años, 1 mes
Puntos: 0
Query Que Me Devuelva La Ultima Fecha

HOLA, NECESITO HACER UNA CONSULTA EN UNA TABLA EN DONDE SE GUARDAN TODOS LOS REGISTROS DE LOS ARTICULOS, BASICAMENTE LA INFORMACION QUE NECESITO ES ULTIMA EXISTENCIA DE LOS ARTICULOS DE UN DIA DETERMINADO POR ENDE SOLO NECESITO QUE ME DEVUELVA UN REGISTRO POR CADA ARTICULO
yo intente con el siguiente sql pero me devuelbe todos los registros

SELECT MAX(BITACORAArticulos.Fecha), CodArticulo, Existencia, CodBodega
FROM BITACORAArticulos
WHERE (dbo.FormatoFecha(BITACORAArticulos.Fecha, '%Y%m%d')= '20070201')
GROUP BY BITACORAArticulos.Fecha, CodArticulo, Existencia, CodBodega
ORDER BY CodArticulo

si alguien sabe cual es la correccion que debo hacer le agrezco toda informacion.
  #2 (permalink)  
Antiguo 07/10/2007, 04:20
Avatar de PosProdukcion  
Fecha de Ingreso: noviembre-2004
Ubicación: Manzanares el Real (Madrid)
Mensajes: 726
Antigüedad: 19 años, 5 meses
Puntos: 9
Re: Query Que Me Devuelva La Ultima Fecha

Ordenas por fecha y seleccionas solo uno con TOP 1, ahora mismo no sabría decirte si selecciona el primero o el último así que tal vez tengas que completar el order by con ASC o DESC (orden ascendente o descendente)...

SELECT TOP 1 BITACORAArticulos.Fecha, CodArticulo, Existencia, CodBodega
FROM BITACORAArticulos
WHERE (dbo.FormatoFecha(BITACORAArticulos.Fecha, '%Y%m%d')= '20070201')
GROUP BY BITACORAArticulos.Fecha, CodArticulo, Existencia, CodBodega
ORDER BY BITACORAArticulos.Fecha
  #3 (permalink)  
Antiguo 07/10/2007, 21:54
 
Fecha de Ingreso: marzo-2007
Mensajes: 76
Antigüedad: 17 años, 1 mes
Puntos: 0
Re: Query Que Me Devuelva La Ultima Fecha

Ordenas por fecha y seleccionas solo uno con TOP 1, ahora mismo no sabría decirte si selecciona el primero o el último así que tal vez tengas que completar el order by con ASC o DESC (orden ascendente o descendente)...

LO QUE PASA ES QUE NO SE SI PLANTEE MAL LA PREGUNTA POR QUE EL TOP 1 SOLO ME VA A DEVOLVER 1 REGISTRO Y YO LO QUE NECESITO SON TODOS LOS REGISTROS QUE EN ESTE CASO ESA TABLA LO QUE GUARDA SON LAS EXISTENCIAS Y LOS MOVIMIENTOS DE LOS ARTICULOS, POR QUE EL QUERY QUE TE MOSTRE ANTERIORMENTE ME DEVUELVE TODOS LOS REGISTROS OSEA QUE SI UN ARTICULO TIENE MAS DE UN MOVIMIENTO MA LO VA A MOSTRAR Y ES LO QUE NO NECESITO, SOLO NECESITO UN REGISTRO POR CADA ARTICULO Y QUE SEA EL ULTIMO MOVIMIENTO DEL DIA QUE YO CONDICIONO
  #4 (permalink)  
Antiguo 08/10/2007, 01:33
Avatar de PosProdukcion  
Fecha de Ingreso: noviembre-2004
Ubicación: Manzanares el Real (Madrid)
Mensajes: 726
Antigüedad: 19 años, 5 meses
Puntos: 9
Re: Query Que Me Devuelva La Ultima Fecha

Bueno, entonces la forma que a mi se me ocurre no se si es la más eficiente, pero ahí va...

Select BITACORAArticulos.Fecha, CodArticulo, Existencia, CodBodega
FROM BITACORAArticulos
WHERE (dbo.FormatoFecha(BITACORAArticulos.Fecha, '%Y%m%d') =
(SELECT MAX(BITACORAArticulos.Fecha) FROM BITACORAArticulos)
--GROUP BY BITACORAArticulos.Fecha, CodArticulo, Existencia, CodBodega
ORDER BY CodArticulo

(en este caso ya no haría falta el group by)
  #5 (permalink)  
Antiguo 08/10/2007, 11:36
 
Fecha de Ingreso: marzo-2007
Mensajes: 76
Antigüedad: 17 años, 1 mes
Puntos: 0
Re: Query Que Me Devuelva La Ultima Fecha

exelente, muchas gracias por tu ayuda
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 02:22.