Solved

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

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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

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

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 …
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

911 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

24 Experts available now in Live!

Get 1:1 Help Now