задача определить исправность /правильность расположения GPS антенны
Code
DECLARE @StartDate datetime
DECLARE @EndDate datetime
SELECT @StartDate = '20111023 08:00', @EndDate ='20111024 08:00'
DECLARE @ObjectID int
DECLARE @ObjectTotalEvents float
DECLARE @ObjectSignalLostEvents float
IF OBJECT_ID('tempdb..#Results') IS NOT NULL DROP TABLE #Results
CREATE TABLE #Results (
ObjectID int, /* ID машины в базе данных BN.dbo.FLEET_Object */
ObjectGosNum varchar(20), /* Гаражный номер машины */
TotalEventsCount float, /* Суммарное количество записей по машине*/
SignalLostEventsCount float, /* Количество записей по машине о потери сигнала*/
SignalLostPercent float /* Процент потерь сигнала по машине*/
)
DECLARE curs_object CURSOR FOR
SELECT ObjectID FROM [BN].[dbo].[FLEET_Object]
WHERE ObjectActive = 1
OPEN curs_object
FETCH NEXT FROM curs_object INTO @ObjectID
WHILE (@@FETCH_STATUS = 0)
BEGIN
SELECT @ObjectTotalEvents = 0, @ObjectSignalLostEvents = 0
SELECT @ObjectTotalEvents = COUNT(tele_time)
FROM [BN].[dbo].[SYS_DEV_ArchiveData] with(index(IX2_SYS_DEV_ArchiveData), readpast)
WHERE
ObjectID = @ObjectID
AND tele_time BETWEEN @StartDate AND @EndDate
AND digit_sens_num NOT IN (544, 528, 576, 33280)
SELECT @ObjectSignalLostEvents = COUNT(tele_time)
FROM [BN].[dbo].[SYS_DEV_ArchiveData] with(index(IX2_SYS_DEV_ArchiveData), readpast)
WHERE
ObjectID = @ObjectID
AND tele_time BETWEEN @StartDate AND @EndDate
AND digit_sens_num = 512
IF @ObjectTotalEvents <> 0
INSERT INTO #Results
SELECT
ObjectID, ObjectGarNum, @ObjectTotalEvents,
@ObjectSignalLostEvents, ROUND(@ObjectSignalLostEvents / @ObjectTotalEvents * 100, 3)
FROM [BN].[dbo].[FLEET_Object]
WHERE ObjectActive = 1 AND ObjectID = @ObjectID
FETCH NEXT FROM curs_object INTO @ObjectID
END
CLOSE curs_object
DEALLOCATE curs_object
SELECT * FROM #Results
ORDER BY ObjectGosNum
IF OBJECT_ID('tempdb..#Results') IS NOT NULL DROP TABLE #Results
имеем в итоге
ObjectID ObjectGosNum TotalEventsCount SignalLostEventsCount SignalLostPercent
62_________IVECO 10_______703______________14_________________________1,991