Monday, March 19, 2012

List when Changes occur

Hi I am a newbie to SQL.

I have a historical list of digatal points listed by time.ie: 3 fields
PointName;Date/Time;State.

I need to return a list of When a specific point chsnges state.

For example a list everytime Point A transitions to State 1.

Any help is appreciated.

--
Posted via http://dbforums.comAssuming your table looks like this:

CREATE TABLE PointStates (pointname CHAR(1), dt DATETIME, state INTEGER NOT
NULL, PRIMARY KEY (pointname,dt))

You can use this query:

SELECT DISTINCT
MIN(B.dt)
FROM PointStates AS A
JOIN PointStates AS B
ON A.pointname = B.pointname
AND A.dt < B.dt
AND A.state<>B.state
GROUP BY A.pointname, A.dt

--
David Portas
----
Please reply only to the newsgroup
--

No comments:

Post a Comment