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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:16:26+00:00 2026-05-25T20:16:26+00:00

I have a table containing device movements. MoveID DeviceID Start End I want to

  • 0

I have a table containing device movements.

MoveID DeviceID Start End

I want to find out if there is a way to sum up the total movement days for each device to the present. However if there is a gap 6 weeks bewtween an end date and the next start date then the time count is reset.

MoveID    DeviceID    Start     End
1           1         2011-1-1  2011-2-1
2           1         2011-9-1  2011-9-20
3           1         2011-9-25 2011-9-28

The total for device should be 24 days as because there is a gap of greater than 6 weeks. Also I’d like to find out the number of days since the first movement in the group in this case 28 days as the latest count group started on the 2011-9-1

I thought I could do it with a stored proc and a cursor etc (which is not good) just wondered if there was anything better?

Thanks

Graeme

  • 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-25T20:16:26+00:00Added an answer on May 25, 2026 at 8:16 pm
    DECLARE @Times TABLE
    (
        MoveID INT,
        DeviceID INT,
        Start DATETIME,
        [End] DATETIME
    )
    
    INSERT INTO @Times VALUES (1, 1, '1/1/2011', '2/1/2011')
    INSERT INTO @Times VALUES (2, 1, '9/1/2011', '9/20/2011')
    INSERT INTO @Times VALUES (3, 1, '9/25/2011', '9/28/2011')
    
    INSERT INTO @Times VALUES (4, 2, '1/1/2011', '2/1/2011')
    INSERT INTO @Times VALUES (5, 2, '3/1/2011', '4/20/2011')
    INSERT INTO @Times VALUES (6, 2, '5/1/2011', '6/20/2011')
    
    DECLARE @MaxGapInWeeks INT
    SET @MaxGapInWeeks = 6
    
    SELECT
        validTimes.DeviceID,
        SUM(DATEDIFF(DAY, validTimes.Start, validTimes.[End]) + 1) AS TotalDays,
        DATEDIFF(DAY, MIN(validTimes.Start), MAX(validTimes.[End])) + 1 AS TotalDaysInGroup
    FROM 
        @Times validTimes LEFT JOIN
        @Times timeGap 
            ON timeGap.DeviceID = validTimes.DeviceID
            AND timeGap.MoveID <> validTimes.MoveID
            AND DATEDIFF(WEEK, validTimes.[End], timeGap.Start) > @MaxGapInWeeks
    WHERE timeGap.MoveID IS NULL
    GROUP BY validTimes.DeviceID
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table containing 10 records. I want to fetch 5th and the
I have a table containing attributes Id, Emp_name, dept_name, salary . Now i want
I have a table containing multiple addresses, including their Lat/Long coordinates, and I want
I have a table containing IP Address,timestamp and browser columns.I need to find the
I have a table containing a field called ChannelId. I want to break it
I have a table containing users... one way or another some users were doubled
I have a table containing a number of timestamps per day, they represents start
I have table containing data . In every row there is a checkbox plus
I have a table containing prices for a lot of different things in a
I have a table containing the runtimes for generators on different sites, and I

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.