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