Calculate time difference based on condition - SQL Server

  • Dear All,

    My requirement is to calculate the failure time, I just want to know the difference in hours where the status is 'I' and it shouldnt calculate if status is A

    CREATE TABLE [dbo].[Temp2](

    [SiteCode] [int] NULL,

    [StatusTime] [datetime] NULL,

    [Status] [char](2) NULL

    ) ON [PRIMARY]

    GO

    SET ANSI_PADDING OFF

    GO

    insert into Temp2 values(111,'2016-05-30 13:02:00.000','A')

    insert into Temp2 values(111,'2016-05-30 14:02:00.000','I')

    insert into Temp2 values(111,'2016-05-30 15:02:00.000','I')

    insert into Temp2 values(112,'2016-05-30 19:17:00.000','A')

    insert into Temp2 values(112,'2016-05-30 21:02:00.000','I')

    insert into Temp2 values(112,'2016-05-30 23:02:00.000','I')

    Consider I as InActive and A as Active, So if i pull the records for 'I' then the Result is expected for the code 111 is 1 Hour as and for the code 112 its 2 hours, My real scenario is different I just put sample table and records

    Please help me

  • I gave response on your other thread.....please dont double post

    http://www.sqlservercentral.com/Forums/FindPost1790784.aspx

    ________________________________________________________________
    you can lead a user to data....but you cannot make them think
    and remember....every day is a school day

  • Sorry,

    I replied back

  • you got the answer or you are still looking for the answer

  • subramanian.esakkimuthu 40485 (6/2/2016)


    you got the answer or you are still looking for the answer

    Please use the other thread that JLS provided. This one is a duplicate..

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply