Solved

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

Posted on 2013-11-21
2
361 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 500 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

Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

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…
Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

762 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now