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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:19:27+00:00 2026-05-25T10:19:27+00:00

This is SQL Server 2000 so I don’t have any windowing functions (row_number). I

  • 0

This is SQL Server 2000 so I don’t have any windowing functions (row_number).

I have a table emp_data :

emp_id     datime                 miles    gallons
23148      2011-08-21 02:00        32       3
23148      2011-08-21 09:00        38       4
23148      2011-08-21 11:00        40       5
42938      2011-08-20 03:00        23       1
42938      2011-08-22 08:00        53       13
  • Each row is cumulative (running?) from the previous one.

I need to get the number of miles driven by the employee, which I do by subtracting the miles for the earliest date minus the miles for the latest date. (40-32 = 8 miles driven for empid=23148). I need to do this for gallons too.

I need to calculate miles per gallon for each driver.

The end result should be this:

  emp_id    miles   gallons
   23148      8       2
   42938      30      12

Doing it for multiple drivers is where I’m stuck. In SQL Server 2005, I could probably do row_number partition_by, but don’t know what to do in SQL Server 2000. I’ve done something like this for one driver. Won’t work partitioned by drivers. Had to use identity() in place of row_number.

SELECT IDENTITY(int) as id, emp_id, datime, miles, gallons
into #t1
FROM emp_data
where 
   emp_id='18018'
   and datime >= '20110820 02:00'
   and datime <= '20110827 02:00'
ORDER BY datime

select foo1.emp_id,foo2.miles - foo1.miles as miles_driven,
    foo2.gallons - foo2.gallons as gallons_used
from (
    SELECT * 
    FROM #t1
    where id = 1) foo1
CROSS JOIN  (
    SELECT *
    from #t1 
    where id = (select max(id) from #t1 t)
    ) foo2

I do have a linked server to the SQL Server 2000 db from SQL Server 2008 so I’m thinking of getting the data and then processing there, but there’s about 1 million records for just one week. I might need to do this for YTD.

Let me know if something is unclear. Sorry I don’t have any sample data.

  • 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-25T10:19:28+00:00Added an answer on May 25, 2026 at 10:19 am

    I think this is what you’re looking for (no need for temp tables) but only use if you have additional data constraints that we’re not seeing. Otherwise go with smdrager answer

    SELECT minmaxdate.emp_id, 
           LAST.miles - FIRST.miles, 
           LAST.gallons - FIRST.gallons 
    FROM   (SELECT emp_id, 
                   MIN(datime) firstdate, 
                   MAX(datime) lastdate 
            FROM   emp_data 
            GROUP  BY emp_id) minmaxdate 
           INNER JOIN emp_data FIRST 
             ON FIRST.emp_id = minmaxdate.emp_id 
                AND FIRST.datime = minmaxdate.firstdate 
           INNER JOIN emp_data LAST 
             ON FIRST.emp_id = minmaxdate.emp_id 
                AND LAST.datime = minmaxdate.lastdate 
    
    • 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 SQL Server 2000 Standard Edition called dbo.T668 (don't blame
I have this query in sql server 2000: select pwdencrypt('AAAA') which outputs an encrypted
I have this recorded in SQL Server: 1- startTime (datetime): 5/2/2009 08:30 (brazilian time
I have an old SQL Server 2000 database that I want to get into
We are using SQL Server 2000 and have to move data across servers to
Need help with this SQL Server 2000 procedure. The problem is made difficult because
Using SQl Server 2000 I have a stored procedure that joins 2 tables and
I have a SQL Server 2000 box that houses several databases, some of which
I have to migrate existing data which is in SQL Server 2000 to SQL
I have an application which connects to SQL Server 2000 (using a generic SQL

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.