Buscar la fecha mayor dentro de periodos de fechas.

25/10/2008 - 20:11 por Tomas | Informe spam
Hola a todos.

Tengo un problema con el filtro de fechas que no termino de encontrar la
solución.

Utilizo SQL Server 2000, donde tengo una tabla de histórico de precios y
necesito conocer, el último precio asignado en cada punto de venta, para un
articulo determinado y durante que periodo de tiempo.

Por ejemplo, si buscamos el último precio asignado al articulo 1 en cada
punto de venta, de los datos de muestra que acompaño, debería obtener una
lista como la siguiente:

Tienda Articulo Inicio Final Precio

1 1 2008-11-20 2008-12-11 4.95
2 1 2008-11-20 2008-12-11 4.95
3 1 2008-11-20 2008-12-11 4.95
4 1 2008-11-20 2008-12-11 4.95
5 1 2008-11-20 2008-12-11 4.95
6 1 2008-08-04 2008-08-17 5.95
7 1 2008-11-20 2008-12-11 4.95
13 1 2008-08-04 2008-08-17 5.95
14 1 2008-08-04 2008-08-17 5.95
15 1 2008-11-20 2008-12-11 4.95
16 1 2008-11-20 2008-12-11 4.95
17 1 2008-11-20 2008-12-11 4.95


Gracias por anticipado.
Tomás.


Estos son los datos de ejemplo.


CREATE TABLE [Pruebas] (
[RegistroID] [int] IDENTITY (1, 1) NOT NULL ,
[Tienda] [int] NOT NULL CONSTRAINT [DF_Pruebas_Tienda] DEFAULT (0),
[Articulo] [int] NOT NULL CONSTRAINT [DF_Pruebas_Articulo] DEFAULT (0),
[Inicio] [datetime] NOT NULL CONSTRAINT [DF_Pruebas_Inicio] DEFAULT
(getdate()),
[Final] [datetime] NOT NULL CONSTRAINT [DF_Pruebas_Final] DEFAULT
(getdate()),
[Precio] [numeric](18, 2) NOT NULL CONSTRAINT [DF_Pruebas_Precio] DEFAULT
(0),
CONSTRAINT [PK_Pruebas] PRIMARY KEY CLUSTERED ([RegistroID]) WITH
FILLFACTOR = 90 ON [PRIMARY]
) ON [PRIMARY]


INSERT INTO dbo.Pruebas VALUES(1,1,'20/11/2008 00:00:00','11/12/2008
00:00:00',4.95)
INSERT INTO dbo.Pruebas VALUES(1,2,'15/09/2008 00:00:00','21/09/2008
00:00:00',4.85)
INSERT INTO dbo.Pruebas VALUES(1,1,'04/08/2008 00:00:00','17/08/2008
00:00:00',5.95)
INSERT INTO dbo.Pruebas VALUES(1,4,'07/07/2008 00:00:00','13/07/2008
00:00:00',4.95)
INSERT INTO dbo.Pruebas VALUES(1,5,'31/01/2008 00:00:00','21/02/2008
00:00:00',6.40)
INSERT INTO dbo.Pruebas VALUES(2,1,'20/11/2008 00:00:00','11/12/2008
00:00:00',4.95)
INSERT INTO dbo.Pruebas VALUES(2,2,'15/09/2008 00:00:00','21/09/2008
00:00:00',4.85)
INSERT INTO dbo.Pruebas VALUES(2,1,'04/08/2008 00:00:00','17/08/2008
00:00:00',5.95)
INSERT INTO dbo.Pruebas VALUES(2,4,'07/07/2008 00:00:00','13/07/2008
00:00:00',4.95)
INSERT INTO dbo.Pruebas VALUES(2,5,'31/01/2008 00:00:00','21/02/2008
00:00:00',6.40)
INSERT INTO dbo.Pruebas VALUES(3,1,'20/11/2008 00:00:00','11/12/2008
00:00:00',4.95)
INSERT INTO dbo.Pruebas VALUES(3,2,'15/09/2008 00:00:00','21/09/2008
00:00:00',4.85)
INSERT INTO dbo.Pruebas VALUES(3,1,'04/08/2008 00:00:00','17/08/2008
00:00:00',5.95)
INSERT INTO dbo.Pruebas VALUES(3,4,'07/07/2008 00:00:00','13/07/2008
00:00:00',4.95)
INSERT INTO dbo.Pruebas VALUES(3,5,'31/01/2008 00:00:00','21/02/2008
00:00:00',6.40)
INSERT INTO dbo.Pruebas VALUES(4,1,'20/11/2008 00:00:00','11/12/2008
00:00:00',4.95)
INSERT INTO dbo.Pruebas VALUES(4,2,'15/09/2008 00:00:00','21/09/2008
00:00:00',4.85)
INSERT INTO dbo.Pruebas VALUES(4,1,'04/08/2008 00:00:00','17/08/2008
00:00:00',5.95)
INSERT INTO dbo.Pruebas VALUES(4,4,'07/07/2008 00:00:00','13/07/2008
00:00:00',4.95)
INSERT INTO dbo.Pruebas VALUES(4,5,'31/01/2008 00:00:00','21/02/2008
00:00:00',6.40)
INSERT INTO dbo.Pruebas VALUES(5,1,'20/11/2008 00:00:00','11/12/2008
00:00:00',4.95)
INSERT INTO dbo.Pruebas VALUES(5,2,'15/09/2008 00:00:00','21/09/2008
00:00:00',4.85)
INSERT INTO dbo.Pruebas VALUES(5,1,'04/08/2008 00:00:00','17/08/2008
00:00:00',5.95)
INSERT INTO dbo.Pruebas VALUES(5,4,'07/07/2008 00:00:00','13/07/2008
00:00:00',4.95)
INSERT INTO dbo.Pruebas VALUES(5,5,'31/01/2008 00:00:00','21/02/2008
00:00:00',6.40)
INSERT INTO dbo.Pruebas VALUES(6,1,'04/08/2008 00:00:00','17/08/2008
00:00:00',5.95)
INSERT INTO dbo.Pruebas VALUES(7,1,'20/11/2008 00:00:00','11/12/2008
00:00:00',4.95)
INSERT INTO dbo.Pruebas VALUES(7,2,'15/09/2008 00:00:00','21/09/2008
00:00:00',4.85)
INSERT INTO dbo.Pruebas VALUES(7,1,'04/08/2008 00:00:00','17/08/2008
00:00:00',5.95)
INSERT INTO dbo.Pruebas VALUES(7,4,'07/07/2008 00:00:00','13/07/2008
00:00:00',4.95)
INSERT INTO dbo.Pruebas VALUES(7,5,'31/01/2008 00:00:00','21/02/2008
00:00:00',6.40)
INSERT INTO dbo.Pruebas VALUES(13,1,'04/08/2008 00:00:00','17/08/2008
00:00:00',5.95)
INSERT INTO dbo.Pruebas VALUES(14,1,'04/08/2008 00:00:00','17/08/2008
00:00:00',5.95)
INSERT INTO dbo.Pruebas VALUES(15,1,'20/11/2008 00:00:00','11/12/2008
00:00:00',4.95)
INSERT INTO dbo.Pruebas VALUES(15,2,'15/09/2008 00:00:00','21/09/2008
00:00:00',4.85)
INSERT INTO dbo.Pruebas VALUES(15,1,'04/08/2008 00:00:00','17/08/2008
00:00:00',5.95)
INSERT INTO dbo.Pruebas VALUES(15,4,'07/07/2008 00:00:00','13/07/2008
00:00:00',4.95)
INSERT INTO dbo.Pruebas VALUES(15,5,'31/01/2008 00:00:00','21/02/2008
00:00:00',6.40)
INSERT INTO dbo.Pruebas VALUES(16,1,'20/11/2008 00:00:00','11/12/2008
00:00:00',4.95)
INSERT INTO dbo.Pruebas VALUES(16,2,'15/09/2008 00:00:00','21/09/2008
00:00:00',4.85)
INSERT INTO dbo.Pruebas VALUES(16,1,'04/08/2008 00:00:00','17/08/2008
00:00:00',5.95)
INSERT INTO dbo.Pruebas VALUES(16,4,'07/07/2008 00:00:00','13/07/2008
00:00:00',4.95)
INSERT INTO dbo.Pruebas VALUES(16,5,'31/01/2008 00:00:00','21/02/2008
00:00:00',6.40)
INSERT INTO dbo.Pruebas VALUES(17,1,'20/11/2008 00:00:00','11/12/2008
00:00:00',4.95)
INSERT INTO dbo.Pruebas VALUES(17,2,'15/09/2008 00:00:00','21/09/2008
00:00:00',4.85)
INSERT INTO dbo.Pruebas VALUES(17,1,'04/08/2008 00:00:00','17/08/2008
00:00:00',5.95)
INSERT INTO dbo.Pruebas VALUES(17,4,'07/07/2008 00:00:00','13/07/2008
00:00:00',4.95)
INSERT INTO dbo.Pruebas VALUES(17,5,'31/01/2008 00:00:00','21/02/2008
00:00:00',6.40)
 

Leer las respuestas

#1 Alejandro Mesa
25/10/2008 - 21:22 | Informe spam
Tomas,

Ve si esto te es de ayuda. La idea es usar una tabla derivada donde se tenga
el articulo, la tienda, y la ultima fecha de ese articulo en esa tienda.
Luego hacemos un inner join con la tabla original para traer las filas que
machan la condicion de union por tienda, articulo y fecha igual a la ultima.

SELECT
a.[RegistroID],
a.[Tienda],
a.[Articulo],
a.[Inicio],
a.[Final],
a.[Precio]
FROM
dbo.[Pruebas] AS a
INNER JOIN
(
SELECT [Tienda], [Articulo], MAX([Final]) AS max_Final
FROM dbo.[Pruebas]
GROUP BY [Tienda], [Articulo]
) AS b
ON a.Tienda = b.Tienda
AND a.Articulo = b.Articulo
AND a.Final = b.max_Final
ORDER BY
a.[Articulo],
a.[Tienda],
a.[Final]
GO

En las versiones a partir de la 2005, contamos con funciones de ranking, las
cuales junto con la clausula OVER, hacen que la sentencia sea mas sencilla.

; with r_set
as
(
select *, row_number() over(partition by Tienda, Articulo order by Final
DESC) as rn
from dbo.Pruebas
)
select *
from r_set
where rn = 1
order by Articulo, Tienda;
GO

Si no interesa ordenar el resultado final, entonces podemos usar

select top 1 with ties *
from dbo.Pruebas
order by row_number() over(partition by Tienda, Articulo order by Final DESC);
GO


AMB



"Tomas" wrote:

Hola a todos.

Tengo un problema con el filtro de fechas que no termino de encontrar la
solución.

Utilizo SQL Server 2000, donde tengo una tabla de histórico de precios y
necesito conocer, el último precio asignado en cada punto de venta, para un
articulo determinado y durante que periodo de tiempo.

Por ejemplo, si buscamos el último precio asignado al articulo 1 en cada
punto de venta, de los datos de muestra que acompaño, debería obtener una
lista como la siguiente:

Tienda Articulo Inicio Final Precio

1 1 2008-11-20 2008-12-11 4.95
2 1 2008-11-20 2008-12-11 4.95
3 1 2008-11-20 2008-12-11 4.95
4 1 2008-11-20 2008-12-11 4.95
5 1 2008-11-20 2008-12-11 4.95
6 1 2008-08-04 2008-08-17 5.95
7 1 2008-11-20 2008-12-11 4.95
13 1 2008-08-04 2008-08-17 5.95
14 1 2008-08-04 2008-08-17 5.95
15 1 2008-11-20 2008-12-11 4.95
16 1 2008-11-20 2008-12-11 4.95
17 1 2008-11-20 2008-12-11 4.95


Gracias por anticipado.
Tomás.


Estos son los datos de ejemplo.


CREATE TABLE [Pruebas] (
[RegistroID] [int] IDENTITY (1, 1) NOT NULL ,
[Tienda] [int] NOT NULL CONSTRAINT [DF_Pruebas_Tienda] DEFAULT (0),
[Articulo] [int] NOT NULL CONSTRAINT [DF_Pruebas_Articulo] DEFAULT (0),
[Inicio] [datetime] NOT NULL CONSTRAINT [DF_Pruebas_Inicio] DEFAULT
(getdate()),
[Final] [datetime] NOT NULL CONSTRAINT [DF_Pruebas_Final] DEFAULT
(getdate()),
[Precio] [numeric](18, 2) NOT NULL CONSTRAINT [DF_Pruebas_Precio] DEFAULT
(0),
CONSTRAINT [PK_Pruebas] PRIMARY KEY CLUSTERED ([RegistroID]) WITH
FILLFACTOR = 90 ON [PRIMARY]
) ON [PRIMARY]


INSERT INTO dbo.Pruebas VALUES(1,1,'20/11/2008 00:00:00','11/12/2008
00:00:00',4.95)
INSERT INTO dbo.Pruebas VALUES(1,2,'15/09/2008 00:00:00','21/09/2008
00:00:00',4.85)
INSERT INTO dbo.Pruebas VALUES(1,1,'04/08/2008 00:00:00','17/08/2008
00:00:00',5.95)
INSERT INTO dbo.Pruebas VALUES(1,4,'07/07/2008 00:00:00','13/07/2008
00:00:00',4.95)
INSERT INTO dbo.Pruebas VALUES(1,5,'31/01/2008 00:00:00','21/02/2008
00:00:00',6.40)
INSERT INTO dbo.Pruebas VALUES(2,1,'20/11/2008 00:00:00','11/12/2008
00:00:00',4.95)
INSERT INTO dbo.Pruebas VALUES(2,2,'15/09/2008 00:00:00','21/09/2008
00:00:00',4.85)
INSERT INTO dbo.Pruebas VALUES(2,1,'04/08/2008 00:00:00','17/08/2008
00:00:00',5.95)
INSERT INTO dbo.Pruebas VALUES(2,4,'07/07/2008 00:00:00','13/07/2008
00:00:00',4.95)
INSERT INTO dbo.Pruebas VALUES(2,5,'31/01/2008 00:00:00','21/02/2008
00:00:00',6.40)
INSERT INTO dbo.Pruebas VALUES(3,1,'20/11/2008 00:00:00','11/12/2008
00:00:00',4.95)
INSERT INTO dbo.Pruebas VALUES(3,2,'15/09/2008 00:00:00','21/09/2008
00:00:00',4.85)
INSERT INTO dbo.Pruebas VALUES(3,1,'04/08/2008 00:00:00','17/08/2008
00:00:00',5.95)
INSERT INTO dbo.Pruebas VALUES(3,4,'07/07/2008 00:00:00','13/07/2008
00:00:00',4.95)
INSERT INTO dbo.Pruebas VALUES(3,5,'31/01/2008 00:00:00','21/02/2008
00:00:00',6.40)
INSERT INTO dbo.Pruebas VALUES(4,1,'20/11/2008 00:00:00','11/12/2008
00:00:00',4.95)
INSERT INTO dbo.Pruebas VALUES(4,2,'15/09/2008 00:00:00','21/09/2008
00:00:00',4.85)
INSERT INTO dbo.Pruebas VALUES(4,1,'04/08/2008 00:00:00','17/08/2008
00:00:00',5.95)
INSERT INTO dbo.Pruebas VALUES(4,4,'07/07/2008 00:00:00','13/07/2008
00:00:00',4.95)
INSERT INTO dbo.Pruebas VALUES(4,5,'31/01/2008 00:00:00','21/02/2008
00:00:00',6.40)
INSERT INTO dbo.Pruebas VALUES(5,1,'20/11/2008 00:00:00','11/12/2008
00:00:00',4.95)
INSERT INTO dbo.Pruebas VALUES(5,2,'15/09/2008 00:00:00','21/09/2008
00:00:00',4.85)
INSERT INTO dbo.Pruebas VALUES(5,1,'04/08/2008 00:00:00','17/08/2008
00:00:00',5.95)
INSERT INTO dbo.Pruebas VALUES(5,4,'07/07/2008 00:00:00','13/07/2008
00:00:00',4.95)
INSERT INTO dbo.Pruebas VALUES(5,5,'31/01/2008 00:00:00','21/02/2008
00:00:00',6.40)
INSERT INTO dbo.Pruebas VALUES(6,1,'04/08/2008 00:00:00','17/08/2008
00:00:00',5.95)
INSERT INTO dbo.Pruebas VALUES(7,1,'20/11/2008 00:00:00','11/12/2008
00:00:00',4.95)
INSERT INTO dbo.Pruebas VALUES(7,2,'15/09/2008 00:00:00','21/09/2008
00:00:00',4.85)
INSERT INTO dbo.Pruebas VALUES(7,1,'04/08/2008 00:00:00','17/08/2008
00:00:00',5.95)
INSERT INTO dbo.Pruebas VALUES(7,4,'07/07/2008 00:00:00','13/07/2008
00:00:00',4.95)
INSERT INTO dbo.Pruebas VALUES(7,5,'31/01/2008 00:00:00','21/02/2008
00:00:00',6.40)
INSERT INTO dbo.Pruebas VALUES(13,1,'04/08/2008 00:00:00','17/08/2008
00:00:00',5.95)
INSERT INTO dbo.Pruebas VALUES(14,1,'04/08/2008 00:00:00','17/08/2008
00:00:00',5.95)
INSERT INTO dbo.Pruebas VALUES(15,1,'20/11/2008 00:00:00','11/12/2008
00:00:00',4.95)
INSERT INTO dbo.Pruebas VALUES(15,2,'15/09/2008 00:00:00','21/09/2008
00:00:00',4.85)
INSERT INTO dbo.Pruebas VALUES(15,1,'04/08/2008 00:00:00','17/08/2008
00:00:00',5.95)
INSERT INTO dbo.Pruebas VALUES(15,4,'07/07/2008 00:00:00','13/07/2008
00:00:00',4.95)
INSERT INTO dbo.Pruebas VALUES(15,5,'31/01/2008 00:00:00','21/02/2008
00:00:00',6.40)
INSERT INTO dbo.Pruebas VALUES(16,1,'20/11/2008 00:00:00','11/12/2008
00:00:00',4.95)
INSERT INTO dbo.Pruebas VALUES(16,2,'15/09/2008 00:00:00','21/09/2008
00:00:00',4.85)
INSERT INTO dbo.Pruebas VALUES(16,1,'04/08/2008 00:00:00','17/08/2008
00:00:00',5.95)
INSERT INTO dbo.Pruebas VALUES(16,4,'07/07/2008 00:00:00','13/07/2008
00:00:00',4.95)
INSERT INTO dbo.Pruebas VALUES(16,5,'31/01/2008 00:00:00','21/02/2008
00:00:00',6.40)
INSERT INTO dbo.Pruebas VALUES(17,1,'20/11/2008 00:00:00','11/12/2008
00:00:00',4.95)
INSERT INTO dbo.Pruebas VALUES(17,2,'15/09/2008 00:00:00','21/09/2008
00:00:00',4.85)
INSERT INTO dbo.Pruebas VALUES(17,1,'04/08/2008 00:00:00','17/08/2008
00:00:00',5.95)
INSERT INTO dbo.Pruebas VALUES(17,4,'07/07/2008 00:00:00','13/07/2008
00:00:00',4.95)
INSERT INTO dbo.Pruebas VALUES(17,5,'31/01/2008 00:00:00','21/02/2008
00:00:00',6.40)



Preguntas similares