Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7218175
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T21:26:55+00:00 2026-05-28T21:26:55+00:00

In a SQL Server 2005 database I have a table with data logged when

  • 0

In a SQL Server 2005 database I have a table with data logged when a signal is enabled or disabled. Simplified it looks like this:

Signal  State       Time
------  --------    ----------------
Alarm1  Disabled    2011-11-15 09:00
Alarm1  Enabled     2011-12-20 13:30
Alarm2  Enabled     2011-11-17 15:01
Alarm2  Disabled    2011-12-15 06:57
Alarm3  Disabled    2011-11-10 11:42
Alarm3  Enabled     2011-12-02 13:12
Alarm3  Disabled    2011-12-24 14:41
Alarm4  Enabled     2011-10-09 13:25
Alarm4  Disabled    2012-01-07 08:29
Alarm5  Disabled    2011-11-19 07:12
Alarm5  Enabled     2011-11-28 15:48
Alarm6  Disabled    2011-12-14 17:29
Alarm6  Enabled     2011-12-23 23:46

(All rows also have an ID column with a uniqueidentifier.)

Using T-SQL I want to extract information on which alarms was disabled during a given time frame, e.g. December 2011. The result should not only consist of those alarms that were logged as disabled that month, but also include alarms that were disabled earlier and not enabled during the given time frame.

Given the data above I would like to extract something like:

Signal  Disabled            Enabled
------  ----------------    -------
Alarm1  -                   2011-12-20 12:30
Alarm2  2011-12-15 06:57    -
Alarm3  -                   2011-12-02 13:12
Alarm3  2011-12-24 14:41    -
Alarm6  2011-12-14 17:29    2011-12-23 23:46

Any help appreciated!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T21:26:56+00:00Added an answer on May 28, 2026 at 9:26 pm

    Simplified types.

    DECLARE @t TABLE (AlarmId INT NOT NULL, State BIT NOT NULL, TIME SMALLDATETIME NOT NULL)
    
    INSERT @t
    VALUES
    (1  ,0,'2011-11-15 09:00')
    ,(1  ,1,'2011-12-20 13:30')
    ,(2  ,1,'2011-11-17 15:01')
    ,(2  ,0,'2011-12-15 06:57')
    ,(3  ,0,'2011-11-10 11:42')
    ,(3  ,1,'2011-12-02 13:12')
    ,(3  ,0,'2011-12-24 14:41')
    ,(4  ,1,'2011-10-09 13:25')
    ,(4  ,0,'2012-01-07 08:29')
    ,(5  ,0,'2011-11-19 07:12')
    ,(5  ,1,'2011-11-28 15:48')
    ,(6  ,0,'2011-12-14 17:29')
    ,(6  ,1,'2011-12-23 23:46')
    
    
    DECLARE @Start DATETIME = '20111201'
    
    --Initial State DISABLED
    ;WITH Disabled(AlarmId, Time) AS
    (
        SELECT 
            AlarmId,
            Time
        FROM
        (
            SELECT 
                ROW_NUMBER() OVER (PARTITION BY AlarmId ORDER BY Time DESC) Ordinal,
                AlarmId,
                Time,
                State
            FROM @t
            WHERE 
                Time < @Start
        ) t
        WHERE 
            t.Ordinal = 1
        AND t.STate = 0
        UNION
        SELECT
            AlarmId,
            TIME
        FROM @t
        WHERE 
            Time >= @start AND Time < DATEADD(m,1,@STart)
        AND STate = 0
    
    ),
    Enabled(AlarmId, Time) AS
    (
        SELECT 
            AlarmId,
            TIME
        FROM @t
        WHERE 
            Time >= @start AND Time < DATEADD(m,1,@STart)
        AND STate = 1
    ),
    Alarms(AlarmId) AS
    (
        SELECT DISTINCT AlarmId FROM @t
    )
    
    SELECT 
        Alarms.AlarmId,
        CASE WHEN Disabled.Time >= @start AND Disabled.Time < DATEADD(m,1,@STart) THEN Disabled.Time ELSE NULL END Disabled,
        CASE WHEN Enabled.Time < Disabled.Time THEN NULL ELSE Enabled.Time END Enabled
    FROM Alarms 
    LEFT JOIN Enabled
        ON Alarms.AlarmId = Enabled.AlarmId
    LEFT JOIN Disabled
        ON Alarms.AlarmId = Disabled.AlarmId
    WHERE COALESCE(Enabled.Time, Disabled.Time) IS NOT NULL
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table in a SQL Server 2005 database that logs purchases like
I have a SQL Server 2005 database. I am logging data to a table.
I have a table in a SQL Server 2005 database with a trigger that
I have a junction table in my SQL Server 2005 database that consist of
I have a following scenario in my SQL Server 2005 database. zipcodes table has
I am using sql server 2005 I have a table [say tblHistory] and this
We have a giant SQL Server 2005 database (75GB) which basically is just data
Question In SQL Server 2005, I have a table with images (data type: image).
I have a .net application using sql server 2005 database. This is a very
I have created views on the SQL Server 2005 database and this view is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.