Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SQL Server 2008- Help calculating total for a day - group by day

Posted on 2013-11-21
2
Medium Priority
?
370 Views
Last Modified: 2013-11-21
Hi.. I need assistance , on how I can calculate the total miles driven for a given day by a car.

I need the total miles driven in a day for each Car

Here's what the data looks like

Car         DateTime             Job #                 Odometer Reading at End of Job
FORD      11/21/2013          1                               67
FORD       11/21/2013        2                               239
FORD         11/21/2013        3                             500
FORD          11/22/2013        4                            700
FORD          11/22/2013         5                          1100


The result I'm looking for would look like this

Car           DateTime                                  Miles Driven

FORD      11/21/2013                                   500
FORD     11/22/2013                                   600


thx!
0
Comment
Question by:JElster
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 
LVL 11

Accepted Solution

by:
Louis01 earned 2000 total points
ID: 39665943
Assuming All starts at 0:
declare @logbook table (Car varchar(50), LogDate DateTime, Job_no int, OdometerReadingEOJ int);

insert into @logbook values ('FORD', '21-Nov-2013', 1, 67);
insert into @logbook values ('FORD', '21-Nov-2013', 2, 239);
insert into @logbook values ('FORD', '21-Nov-2013', 3, 500);
insert into @logbook values ('FORD', '22-Nov-2013', 4, 700);
insert into @logbook values ('FORD', '22-Nov-2013', 5, 1100);



select t1.Car
     , t1.LogDate
     , MAX(t1.OdometerReadingEOJ) - IsNull((SELECT top 1 OdometerReadingEOJ FROM @logbook i2 WHERE i2.Car = t1.Car AND i2.LogDate < t1.LogDate ORDER BY i2.Job_no desc), 0)
  from @logbook t1
 group by t1.Car, t1.LogDate
 order by t1.Car, t1.LogDate

Open in new window

0
 
LVL 11

Expert Comment

by:Louis01
ID: 39665994
Thanks J
Just one more note: This SQL also assumes that your Job No is numeric and get sequentially bigger with time. Hope that is the case?
0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question