sql - How can I discover and compensate for possible flaws in the method of date storage? -


i have list of items have states within date ranges, , counts coming wrong (technically correct, wrong in intent).

i'm wondering if there's examples of date ranges not matching closely on datetimes i'm checking, or if datetimes i'm querying on happen fall holes in date ranges between rows.

the table looks this:

table_above itemid | start                   | stop                    | state 1234   | 2000-01-01 00:00:00.000 | 2014-02-01 10:04:00.000 | 1 1234   | 2014-02-01 10:04:00.003 | null                    | 2 1111   | 2000-01-01 00:00:00.000 | null                    | 2 

itemid 1234 @ state 1 14 years, on feb 1 switched state 2, , stop = null means moment, item 1234 in state 2.

item 1111 has been in state 2 since 2000-01-01 00:00:00.000, , in state 2 currently.

there tiny period of time when db recorded no state @ itemid 1234 following 2 datetimes: '2014-02-01 10:04:00.001' , '2014-02-01 10:04:00.002'

since counts aren't coming quite way expected, i'd check whether it's because of issue.

the query run counts number of items in particular state @ particular datetime:

select   count(1)   start < '2014-02-01 12:00:00.000'   , (stop >= '2014-02-01 12:00:00.000' or stop null)   , state = 2   table_above 

this return 2 table_above. however, following query return 1.

select   count(1)   start < '2014-02-01 10:04:00.001'   , (stop >= '2014-02-01 10:04:00.001' or stop null)   , state = 2   table_above 

the problem in reality, tiny period of time, items associated itemids exist, they're not being counted anywhere.

the query technically correct, need represent intention instead.

if fall gaps, i'd count them state in prior gaps.

also, there way can run query print gaps particular itemid?

finally, i'm suspicious whether there overlap in date ranges there shouldn't be, , i'd query discover whether there overlap. @ given time, item can in 1 state @ time, i'd check whether there points items have multiple states, such in example below.

table_above itemid | start                   | stop                    | state 1234   | 2000-01-01 00:00:00.000 | 2014-02-01 10:04:00.000 | 1 1234   | 2014-02-01 10:03:59.999 | null                    | 2 

if ran query above twice, state = 2 or state = 1 time point '2014-02-01 10:04:00.000', both return count of 1, intended each item have 1 state @ each point in time, should not both return 1

the other answer have useful information, here other thing consider also:

  • if you're using datetime type, values always going rounded increments of .000, .003, or .007, because that's it's precision. in documentation.

  • comparing datetime string cast string datetime, there no "tiny period" of unrepresentation. consider following:

    declare @dt0 datetime, @dt1 datetime, @dt2 datetime, @dt3 datetime select @dt0 = '2014-02-01 10:04:00.000',        @dt1 = '2014-02-01 10:04:00.001',        @dt2 = '2014-02-01 10:04:00.002',        @dt3 = '2014-02-01 10:04:00.003'  if @dt0 = @dt1 print 'true' else print 'false' if @dt2 = @dt3 print 'true' else print 'false' 

    both tests print true because of rounding happening datetime. if need more precise values, consider using datetime2 instead.

  • your logic bit off range comparison. usually, start of range inclusive while end of range exclusive. have reversed.

    instead of:

    start < '2014-02-01 12:00:00.000' , (stop >= '2014-02-01 12:00:00.000' or stop null) 

    it should be:

    start <= '2014-02-01 12:00:00.000' , (stop > '2014-02-01 12:00:00.000' or stop null) 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

jsf - How to ajax update an item in the footer of a PrimeFaces dataTable? -

django - CSRF verification failed. Request aborted. CSRF cookie not set -