[import] Extracting date and time of first history stored for a point
>>Message imported from previous forum - Category:Scripts and Tips<< User: Curious, originally posted: 2019-06-20 04:09:03 Id:455 I would like to Search through the point history of a site and extract the very first date and time that history starting collecting for the site. Does anyone have an SQL query that can do this?
Reply User: adamwoodland, posted: 2019-06-20 23:00:16 How efficient do you need this to be?
If inefficient then you could just do "SELECT TOP(1) * FROM CDBHistoric WHERE Id = [your object id] ORDER BY RecordTime ASC", it might take a long time depending on how much data and how dense the data is. CDBHistoric is disk resident so performance also depends on disk speed (and is relatively slow).
If you need it to be efficient (i.e. multiple points or needing to run often), then you should first query CDBHistoricFile (which is memory resident), look for the oldest granule for that point id with at least one record, and then just query that specific granule for its oldest record, i.e.:
SELECT TOP( 1 ) FILEID, FILETYPE, STARTTIME FROM CDBHISTORICFILE WHERE OBJECTID = 3793274 AND RECORDCOUNT = 1 AND FILETYPE = 0 ORDER BY "StartTime" ASC
Then for the next query:
SELECT TOP( 1 ) RECORDTIME, VALUE FROM CDBHISTORIC WHERE ID = 3793274 AND FILEID = '0039E17A0000554AR' ORDER BY "RecordTime" ASC
(Where 0039E17A0000554AR is the FileId from the first query)