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

  • Home
  • SEARCH
  • 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 756261
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T15:13:29+00:00 2026-05-14T15:13:29+00:00

Its my first post and I’m really rusty on MSSQL so be gentle :-)

  • 0

Its my first post and I’m really rusty on MSSQL so be gentle 🙂

I have a table in which I am trying to use datediff. I think it will be easiest if I post the query and results first

select mh.agetime, mh.whatid

from mailhistory mh
inner join mail m
 on mh.mailid=m.myid
where (mh.whatid=17 or mh.whatid=11 or mh.whatid=0) and maincontactid=287816 and mailid=276086

order by agetime

Really, the maincontactid and mailid are currently just in there to limit the results while i make the query.

The results are as follows…

AGETIME                    WHATID
1899-12-30 00:00:00.000 0
1899-12-30 00:48:10.000 11
1899-12-31 02:16:49.000 17
1899-12-31 06:29:08.000 11
1900-01-18 15:31:40.000 17
1900-02-11 14:56:59.000 11

I am trying to make a third column as the query runs that will make a third column showing the difference in the dates (in days)… between items with a WHATID of 11 and 17… so I’m after results like this:

AGETIME                    WHATID    DIFFERENCE
1899-12-30 00:00:00.000 0         NULL
1899-12-30 00:48:10.000 11        0
1899-12-31 02:16:49.000 17        1
1899-12-31 06:29:08.000 11        0
1900-01-18 15:31:40.000 17        18
1900-02-11 14:56:59.000 11        22

Something like that… So, is there a way to convert my query to do the running datediff like that?

Many thanks in advance!

Chris

  • 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-14T15:13:29+00:00Added an answer on May 14, 2026 at 3:13 pm

    If using SQL Server 2005 or above, you can make a CTE and assign row_number to your current resultset. Then you can have a left self join on CTE with Current.Row_Num = Previous.Row_Num -1 and then can get the date difference. Approach will be similar as show in this link:

    http://www.kodyaz.com/articles/sql-select-previous-and-next-rows-with-current-row-in-tsql.aspx

    For SQL Server 2000

    Assuming there is no grouping in result set:

    I would probably create a temp table variable instead of CTE with an Identity Filed( Which will act as a rownumber) and then will apply the same logic as described above for 2005. Only major change will be instead of CTE we will use Temp Table variable and instead of rownumber we will use identity in temp table.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to write a JavaScript function that will return its first argument(function)
this is my first post on this great source of programming information. I have
this is my first post here! I'm trying to make a windows forms program
I'm trying to write a post-commit hook for SVN, which is hosted on our
This is my first post, have been a lurker for a long time, so
I have a postgresql table storing posts from an online forum. Each post belongs
When programming by contract a function or method first checks whether its preconditions are
PHP (among others) will execute the deepest function first, working its way out. For
Its possible I am just really really thick. However, looking over the SDK for
First post on stackoverflow. Hope everything is right! I'm thinking of attaching an ID

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.