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 6739817
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:31:59+00:00 2026-05-26T11:31:59+00:00

I have a BreakdownLog table with the following columns: EquipmentID, ProblemID, BreakdownDate, IssueFixedDate Each

  • 0

I have a BreakdownLog table with the following columns:

EquipmentID, ProblemID, BreakdownDate, IssueFixedDate

Each equipment can have multiple breakdowns, and more importantly, there could be overlapping date ranges for the same equipment!

For example, given below data:

EquipmentID|ProblemID|BreakdownDate|IssueFixedDate
1          |1        |01-Jun-2011  |01-Sep-2011
1          |2        |01-Jun-2011  |01-Oct-2011
2          |1        |01-Jun-2011  |01-Oct-2011
2          |2        |01-Jun-2011  |01-Oct-2011
3          |1        |15-Jun-2011  |01-Sep-2011
3          |2        |10-Jun-2011  |25-Aug-2011
4          |1        |01-Jun-2011  |01-Aug-2011
4          |2        |10-Sep-2011  |22-Oct-2011
5          |1        |01-Jun-2011  |15-Jun-2011
5          |2        |02-Jun-2011  |NULL

Now I want a query which can compute the number of days each equipment was defunct. If IssueFixedDate is NULL, we assume that the equipment is still defunct and therefore compute Defunct days upto current date.

The expected result set should be:

EquipmentID|DefunctDays
1          |122
2          |122
3          |83
4          |103
5          |143

I am using SQL Server 2008. So even CTEs, cursors etc are acceptable.

Thanks
Raghu

  • 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-26T11:31:59+00:00Added an answer on May 26, 2026 at 11:31 am

    This uses master..spt_values as an adhoc auxiliary numbers table. You can create your own dedicated numbers table using one of the techniques here (start the numbering at zero)

    ;WITH Numbers
         AS (SELECT number
             FROM   master..spt_values
             WHERE  type = 'P')
    SELECT EquipmentID,
           COUNT(DISTINCT number + DATEDIFF(DAY,0, BreakdownDate)) - 1 AS DefunctDays
    FROM   BreakdownLog
           JOIN Numbers N
             ON number <= DATEDIFF(DAY, BreakdownDate,
                          ISNULL(IssueFixedDate, GETDATE()))
    GROUP  BY EquipmentID  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have seen multiple posts on this but I can't see any which answer my
Have you managed to get Aptana Studio debugging to work? I tried following this,
Have following listener for keyboard ArrowDown event(it's key code is 40 ): window.onload =
have anyone can tell me what syntax error on this actionscript (actionscript3.0)? var rotY:
Have been following Rails Tutorial by Michael Hart rails version 3.0 on mac OS
Have added the following code to my SQL query: (Note cut down version) DECLARE
Have a photography site that I want to prevent image copying from. How can
Have a look on following application http://itunes.apple.com/us/app/fatbooth/id372268904?mt=8 i think they are using the same
have the following text file: <div> <asp:HyperLinkField HeaderText=Hello World of Regular Expression /> </div>
Have the following scenario. I have a few form, which essentially have a few

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.