I have a table named Logs:
->OCCUR_TIME --date and time
->NAME --name of a person
->KIND --the kind of log (eg. 40 means `something`)
->VALUE --the value of the kind of log (eg. 99)
I have to create a query:
SELECT
*
FROM LOGS
WHERE NAME='dude'
ORDER BY KIND, OCCUR_TIME, VALUE;
Now this displays the logs and sorted by kind, then occur time (if occur_time is exactly the same it will then sort by value).
Notes:
- VALUE of a KIND must always be +1
- If not report a problem.
What if for an example there was a problem with the log and after the VALUE 400 the next VALUE is 398?
Example:
Occur_Time | Name | Kind | Value
2012-06-26 15:14:25.407 dude 40 398
2012-06-27 16:55:28.730 dude 40 399
2012-06-30 02:43:26.763 dude 40 400
2012-06-30 05:26:32.673 dude 40 398 <-- data prob. (possible rollback)
2012-06-30 16:35:28.330 dude 40 399 <-- problem continuing
2012-06-20 20:29:51.207 dude 41 100 <-- no prob. bcoz its another kind
2012-06-23 05:50:59.130 guy 40 500 <-- no prob. bcoz its another name
I want a query that will find the problem, and where it started. Like this?
Please help. Thank you.
In SQL Server 2012, you would do this using the lag() function.
I’ll assume you are using an earlier version, but post 2005. You can do this with nested windows functions.
The idea is to use row_number() to generate a sequence of numbers. The difference between the value and the sequence should be a constant. The outer query looks at the min and max values of these. If they are different, you have a problem.