I have an applicaton in which I collect data for different parameters
for a set of devices. The data are entered into a single table, each
set of name, value pairs time-stamped and associated with a device.
The definition of the table is as follows:
CREATE TABLE devicedata
(
device_id int NOT NULL REFERENCES devices(id), -- id in the device
table
datetime datetime PRIMARY KEY CLUSTERED, -- date created
name nvarchar(256) NOT NULL, -- name of the attribute
value sql_variant NOT NULL -- value
)
For example, I have 3 devices, and each is monitored for two attributes
-- temperature and pressure. Data for these are gathered at say every
20 minute and every 15 minute intervals.
The table is filled with records over a period of time, and I can
perform a variety of SQL queries.
I have another requirement which requires me to retrieve the *latest*
values of temperature and pressure for each device.
Ideally, I'd like to use the data I have collected to get this
information, and I suppose I can.
What I need is the SELECT statement to do this.
I'd appreciate it very much, if someone can help provide that.
Conceivably, I could use a SQL server View for making this easier for
some of my users.
One alternate technique I thought was to create another table which I
*update* with the latest value, each time I *insert* into the above
table. But it seems like a waste to do so, and introduces needless
referential integrity issues (minor). Maybe for fast access, that is
the best thing to do.
I have requirements to maintain this data for several months/year or
two, so I am dealing with a large number of samples.
Any help would be appreciated.
(I apologize if this post appears twice)
for a set of devices. The data are entered into a single table, each
set of name, value pairs time-stamped and associated with a device.
The definition of the table is as follows:
CREATE TABLE devicedata
(
device_id int NOT NULL REFERENCES devices(id), -- id in the device
table
datetime datetime PRIMARY KEY CLUSTERED, -- date created
name nvarchar(256) NOT NULL, -- name of the attribute
value sql_variant NOT NULL -- value
)
For example, I have 3 devices, and each is monitored for two attributes
-- temperature and pressure. Data for these are gathered at say every
20 minute and every 15 minute intervals.
The table is filled with records over a period of time, and I can
perform a variety of SQL queries.
I have another requirement which requires me to retrieve the *latest*
values of temperature and pressure for each device.
Ideally, I'd like to use the data I have collected to get this
information, and I suppose I can.
What I need is the SELECT statement to do this.
I'd appreciate it very much, if someone can help provide that.
Conceivably, I could use a SQL server View for making this easier for
some of my users.
One alternate technique I thought was to create another table which I
*update* with the latest value, each time I *insert* into the above
table. But it seems like a waste to do so, and introduces needless
referential integrity issues (minor). Maybe for fast access, that is
the best thing to do.
I have requirements to maintain this data for several months/year or
two, so I am dealing with a large number of samples.
Any help would be appreciated.
(I apologize if this post appears twice)
Comment