sql - Time range with overlapping --- without temporary table result is OK otherwise wrong -


below working sql query calculation of time differences overlapping.
reasons need create view - therefore cannot use temporary table tempalmdb. instead view begins select x.alarmgroup , have use created view qv_alarms . in case output different version temporary table.

the original view contains following colums: alarmgroup, alarmon, alarmoff, priority, ...

alarmon , alarmoff arrival , returning time of alarm. calculation done each alarm group. reduce calculation time per query included clauses alarmgroup , priority (in temporary table). clause group possible in second part .

my questions:
1. why different , wrong results when use part 2 (having replaced 4 times tempalmdb qv_alarms)?
2. how possibly include clause priority in part 2?

-- part 1: temporary table if exists(select * information_schema.tables table_name = 'tempalmdb')    drop table tempalmdb  select top 500 alarmgroup, alarmon, alarmoff tempalmdb qv_alarms    alarmgroup = 'a1_1_alarms'  order alarmgroup, alarmon    --part 2: time difference calculation  select x.alarmgroup, x.coming, x.going, datediff(minute, x.coming, x.going)   minutes (   select  g2.alarmgroup, min(g2.start1) coming, max(g2.going1)      going (      select  g1.alarmgroup, min(g1.start2) start1, max(g1.going2)         going1 (          select t1.alarmgroup, min(t1.alarmon) start2, max(t1.alarmoff)           going2 tempalmdb t1 inner join  tempalmdb t2           on (t1.alarmgroup = t2.alarmgroup , t1.alarmon < t2.alarmoff           , t1.alarmoff > t2.alarmon , not (t1.alarmon = t2.alarmon           , t1.alarmoff = t2.alarmoff) )          group  t1.alarmgroup,            case when t1.alarmon between t2.alarmon , t2.alarmoff         t2.alarmon else t1.alarmon end,          case when t1.alarmoff between t2.alarmon , t2.alarmoff         t2.alarmoff else t1.alarmoff end      ) g1      group g1.alarmgroup, g1.going2) g2      group g2.alarmgroup, g2.start1  union  select u1.alarmgroup, u1.alarmon, u1.alarmoff tempalmdb u1 left join tempalmdb u2  on (u1.alarmgroup = u2.alarmgroup , u1.alarmon < u2.alarmoff  , u1.alarmoff > u2.alarmon  , not (u1.alarmon = u2.alarmon , u1.alarmoff = u2.alarmoff)) u2.alarmgroup null  ) x x.alarmgroup = 'a1_1_alarms'  order  x.alarmgroup, x.coming, x.going 

have tried replacing instances of tempalmdb in part 2 this?

(select top 500 alarmgroup, alarmon, alarmoff qv_alarms alarmgroup = 'a1_1_alarms' order alarmgroup, alarmon) 

Comments

Popular posts from this blog

c++ - Convert big endian to little endian when reading from a binary file -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

unicode - Are email addresses allowed to contain non-alphanumeric characters? -