Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2013-11-21
2
Medium Priority
?
373 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
  • 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

This is basically a blog post I wrote recently. I've found that SARGability is poorly understood, and since many people don't read blogs, I figured I'd post it here as an article. SARGable is an adjective in SQL that means that an item can be fou…
After restoring a Microsoft SQL Server database (.bak) from backup or attaching .mdf file, you may run into "Error '15023' User or role already exists in the current database" when you use the "User Mapping" SQL Management Studio functionality to al…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…

783 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