>>Message imported from previous forum - Category:Scripts and Tips<<
User: <None>, 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?
Solved! Go to Solution.
>>Responses imported from previous forum
Reply From 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)
>>Responses imported from previous forum
Reply From 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)
User | Count |
---|---|
188 | |
53 | |
16 | |
16 | |
15 |
Discuss challenges in energy and automation with 30,000+ experts and peers.
Find answers in 10,000+ support articles to help solve your product and business challenges.
Find peer based solutions to your questions. Provide answers for fellow community members!