Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. finding periods

finding periods

Scheduled Pinned Locked Moved Database
databasehelpquestionannouncementcareer
2 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    joost versteegen
    wrote on last edited by
    #1

    given a table job with columns time_utc (datetime) and state (integer), i want to find the periods (start-time and end_time) where the consequetive value of the state column equals 4. i tried to find the start of the periods with the folllowing query, but no success. too many results. can someone please help me? i cannot figure it out.

    SELECT act_start_time_utc, state_cd FROM SRPMES901007.WWMESDB.dbo.job WHERE act_start_time_utc IS NOT NULL ORDER BY act_start_time_utc ASC

    DECLARE @PERIOD TABLE (start_time DATETIME, end_time DATETIME)

    INSERT INTO @PERIOD(start_time)
    SELECT t.start_time/*, t.new_state, t.last_state*/ FROM (
    SELECT act_start_time_utc AS start_time, state_cd AS new_state, LAG(state_cd, 1) OVER (ORDER BY act_start_time_utc ASC) AS last_state
    FROM SRPMES901007.WWMESDB.dbo.job
    WHERE act_start_time_utc IS NOT NULL) AS t
    WHERE t.new_state = 4 AND t.last_state <> 4

    UPDATE period
    SET end_time = (SELECT MIN(act_start_time_utc) endtime FROM SRPMES901007.WWMESDB.dbo.job INNER JOIN @PERIOD period ON job.act_start_time_utc > period.start_time AND state_cd <> 4)
    FROM @PERIOD period

    SELECT * FROM @PERIOD ORDER BY start_time ASC

    J 1 Reply Last reply
    0
    • J joost versteegen

      given a table job with columns time_utc (datetime) and state (integer), i want to find the periods (start-time and end_time) where the consequetive value of the state column equals 4. i tried to find the start of the periods with the folllowing query, but no success. too many results. can someone please help me? i cannot figure it out.

      SELECT act_start_time_utc, state_cd FROM SRPMES901007.WWMESDB.dbo.job WHERE act_start_time_utc IS NOT NULL ORDER BY act_start_time_utc ASC

      DECLARE @PERIOD TABLE (start_time DATETIME, end_time DATETIME)

      INSERT INTO @PERIOD(start_time)
      SELECT t.start_time/*, t.new_state, t.last_state*/ FROM (
      SELECT act_start_time_utc AS start_time, state_cd AS new_state, LAG(state_cd, 1) OVER (ORDER BY act_start_time_utc ASC) AS last_state
      FROM SRPMES901007.WWMESDB.dbo.job
      WHERE act_start_time_utc IS NOT NULL) AS t
      WHERE t.new_state = 4 AND t.last_state <> 4

      UPDATE period
      SET end_time = (SELECT MIN(act_start_time_utc) endtime FROM SRPMES901007.WWMESDB.dbo.job INNER JOIN @PERIOD period ON job.act_start_time_utc > period.start_time AND state_cd <> 4)
      FROM @PERIOD period

      SELECT * FROM @PERIOD ORDER BY start_time ASC

      J Offline
      J Offline
      joost versteegen
      wrote on last edited by
      #2

      i finally figured it out, here's the result:

      DECLARE @PERIOD TABLE (start_time DATETIME, end_time DATETIME)

      INSERT INTO @PERIOD(start_time)
      SELECT t.start_time FROM (
      SELECT act_start_time_utc AS start_time, state_cd AS new_state, LAG(state_cd, 1) OVER (ORDER BY act_start_time_utc ASC) AS last_state
      FROM SRPMES901007.WWMESDB.dbo.job
      WHERE act_start_time_utc IS NOT NULL) AS t
      WHERE t.new_state = 4 AND t.last_state <> 4

      UPDATE period
      SET end_time = (SELECT TOP(1) act_start_time_utc FROM SRPMES901007.WWMESDB.dbo.job WHERE state_cd <> 4 AND act_start_time_utc > period.start_time ORDER BY act_start_time_utc ASC)
      FROM @PERIOD period

      SELECT * FROM @PERIOD ORDER BY start_time ASC

      But the query takes 19 seconds, can it be speeded up?

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups