Saludos!
Tengo un problema con uno de mis SQLs.
La definicion de la tabla sigue:
CREATE TABLE [dbo].[ScEvents] (
[EvDate] [datetime] NULL ,
[EvType] [varchar] (1) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[UsrId] [varchar] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[EvProdId] [varchar] (15) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
) ON [PRIMARY]
GO
Esta se usa para guardar lecturas hechas por medio de un
lector de codigo de barras. Cada vez que el producto se
escanea, se crea un registro conteniendo la fecha, el tipo
de lectura, el id del usuario y el id del producto.
Yo necesito hacer un reporte que indique el numero de
lecturas por persona (usuario). Por lo tanto hice este SQL:
select EvDate,UsrId,EvType,count(*) as iCount
from scevents where
evType in ('S','A') and
evDate Between '2005-12-3' and '2005-12-4'
group by evDate,UsrId,evType
order by evDate,UsrId
El usuario selecciona el rango de fechas.
Este funciona bien, o mejor dicho, casi bien.
Algunos usuarios hacen la lectura mas de una vez para el mismo
producto. Segun me dicen, esto lo hacen porque hacen las lecturas
muy rapido y la doble lectura asegura de que el producto
se haya leido por lo menos una vez. El SQL que hice no contempla
duplicados, los cuales no se deben contar.
Para resolver esto, hice el proceso a mano, o sea en lugar
de un SQL con group by, hice uno mas pequeño que consigue el
conteo de productos distintos:
select Distinct EvProdId from scEvents where
UsrId='ABCD' and "
evDate='yyyy-mm-dd' and "
evType='X'
Este SQL esta dentro de un procedimiento que acepta como
parametros el id del usuario, la fecha y el tipo y se arma
debidamente. Esta tecnica funciona, aunque bastante lenta.
Mi pregunta es si hay alguna forma de hacer un SQL como el
que tenia originalmente que ignore las lecturas duplicadas, asi
esperando agilizar este proceso.
Gracias por su tiempo y atencion.
Saga
Estos Inserts son para agregarle datos a la tabla:
(nota: el tipo (S/A) indica la etapa de lectura tal como
el producto pasa por diferentes manos. En este caso, hay
dos etapas, por lo tanto dos tipos: A y S. Cada producto
puede (y debe) tener por lo menos una lectura de cada
tipo.)
insert into ScEvents values ('2005-12-03','S','FR2346','RD12000')
insert into ScEvents values ('2005-12-03','S','FR2346','QW3020')
insert into ScEvents values ('2005-12-03','S','FR2346','ER45900')
insert into ScEvents values ('2005-12-03','S','FR2346','ED78901')
insert into ScEvents values ('2005-12-03','S','FR2346','UH67339')
insert into ScEvents values ('2005-12-03','S','FR2346','TG23190')
insert into ScEvents values ('2005-12-03','S','FR2346','UJ71393')
insert into ScEvents values ('2005-12-03','S','FR2346','TYF2291')
insert into ScEvents values ('2005-12-03','S','FR2346','TB20089')
insert into ScEvents values ('2005-12-03','S','FR2346','ED32895')
insert into ScEvents values ('2005-12-03','S','DC2399','RT45291')
insert into ScEvents values ('2005-12-03','S','DC2399','GA90122')
insert into ScEvents values ('2005-12-03','S','DC2399','EQ90122')
insert into ScEvents values ('2005-12-03','S','DC2399','AN23190')
insert into ScEvents values ('2005-12-03','S','DC2399','YNA2919')
insert into ScEvents values ('2005-12-03','S','DC2399','JN31900')
insert into ScEvents values ('2005-12-03','A','FR2346','RD12000')
insert into ScEvents values ('2005-12-03','A','FR2346','QW3020')
insert into ScEvents values ('2005-12-03','A','FR2346','ER45900')
insert into ScEvents values ('2005-12-03','A','FR2346','ED78901')
insert into ScEvents values ('2005-12-03','A','FR2346','UH67339')
insert into ScEvents values ('2005-12-03','A','FR2346','TG23190')
insert into ScEvents values ('2005-12-03','A','FR2346','UJ71393')
insert into ScEvents values ('2005-12-03','A','FR2346','TYF2291')
insert into ScEvents values ('2005-12-03','A','FR2346','TB20089')
insert into ScEvents values ('2005-12-03','A','FR2346','ED32895')
insert into ScEvents values ('2005-12-03','A','DC2399','RT45291')
insert into ScEvents values ('2005-12-03','A','DC2399','GA90122')
insert into ScEvents values ('2005-12-03','A','DC2399','EQ90122')
insert into ScEvents values ('2005-12-03','A','DC2399','AN23190')
insert into ScEvents values ('2005-12-03','A','DC2399','YNA2919')
insert into ScEvents values ('2005-12-03','A','DC2399','JN31900')
insert into ScEvents values ('2005-12-04','S','FR2346','RD12G00')
insert into ScEvents values ('2005-12-04','S','FR2346','QW30G0')
insert into ScEvents values ('2005-12-04','S','FR2346','ER45G00')
insert into ScEvents values ('2005-12-04','S','FR2346','ED78G01')
insert into ScEvents values ('2005-12-04','S','FR2346','UH67G39')
insert into ScEvents values ('2005-12-04','S','FR2346','TG23G90')
insert into ScEvents values ('2005-12-04','S','FR2346','UJ71G93')
insert into ScEvents values ('2005-12-04','S','FR2346','TYF2G91')
insert into ScEvents values ('2005-12-04','S','FR2346','TB20G89')
insert into ScEvents values ('2005-12-04','S','FR2346','ED32G95')
insert into ScEvents values ('2005-12-04','S','FR2346','HU27G88')
insert into ScEvents values ('2005-12-04','S','FR2346','YN90G12')
insert into ScEvents values ('2005-12-04','S','FR2346','JQ46G18')
insert into ScEvents values ('2005-12-04','S','DC2399','RT45G91')
insert into ScEvents values ('2005-12-04','S','DC2399','GA90G22')
insert into ScEvents values ('2005-12-04','S','DC2399','EQ90G22')
insert into ScEvents values ('2005-12-04','S','DC2399','AN23G90')
insert into ScEvents values ('2005-12-04','S','DC2399','YNA2G19')
insert into ScEvents values ('2005-12-04','S','DC2399','JN31G00')
insert into ScEvents values ('2005-12-04','S','DC2399','RD29G22')
insert into ScEvents values ('2005-12-04','S','DC2399','NR36G27')
insert into ScEvents values ('2005-12-04','A','FR2346','RD12G00')
insert into ScEvents values ('2005-12-04','A','FR2346','QW30G0')
insert into ScEvents values ('2005-12-04','A','FR2346','ER45G00')
insert into ScEvents values ('2005-12-04','A','FR2346','ED78G01')
insert into ScEvents values ('2005-12-04','A','FR2346','UH67G39')
insert into ScEvents values ('2005-12-04','A','FR2346','TG23G90')
insert into ScEvents values ('2005-12-04','A','FR2346','UJ71G93')
insert into ScEvents values ('2005-12-04','A','FR2346','TYF2G91')
insert into ScEvents values ('2005-12-04','A','FR2346','TB20G89')
insert into ScEvents values ('2005-12-04','A','FR2346','ED32G95')
insert into ScEvents values ('2005-12-04','A','FR2346','HU27G88')
insert into ScEvents values ('2005-12-04','A','FR2346','YN90G12')
insert into ScEvents values ('2005-12-04','A','FR2346','JQ46G18')
insert into ScEvents values ('2005-12-04','A','DC2399','RT45G91')
insert into ScEvents values ('2005-12-04','A','DC2399','GA90G22')
insert into ScEvents values ('2005-12-04','A','DC2399','EQ90G22')
insert into ScEvents values ('2005-12-04','A','DC2399','AN23G90')
insert into ScEvents values ('2005-12-04','A','DC2399','YNA2G19')
insert into ScEvents values ('2005-12-04','A','DC2399','JN31G00')
insert into ScEvents values ('2005-12-04','A','DC2399','RD29G22')
insert into ScEvents values ('2005-12-04','A','DC2399','NR36G27')
insert into ScEvents values ('2005-12-03','S','FR2346','AXX1200')
insert into ScEvents values ('2005-12-03','S','FR2346','AXX1200')
Leer las respuestas