?
Solved

SQL SERVER - Help with subquery - calculate max value in a range

Posted on 2013-11-27
2
Medium Priority
?
346 Views
Last Modified: 2013-12-02
Hi.. I have the following code which is a solution to another question.
I need to include a column that gives me the job with the max miles and number of mles.
Any ideas?  

The result looks like this  (see code below)

CAR   LOGDATE          
FORD     2013-11-21             500
FORD     2013-11-22             600

I NEED TO INCLUDE THE MAX MILES AND JOB NUMBER LIKE THIS

CAR   LOGDATE            TOTAL MILES            JOB   MAX MILES
FORD     2013-11-21             500                   3            261
FORD     2013-11-22             600                   5            400

HERE'S THE CODE

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
0
Comment
Question by:JElster
2 Comments
 
LVL 15

Assisted Solution

by:gplana
gplana earned 1000 total points
ID: 39680742
I don't know the name of the fields of the original tables, but I think you need something like this:

select t1.Car
     , t1.LogDate
     , MAX(t1.OdometerReadingEOJ) - IsNull((SELECT top 1 OdometerReadingEOJ
     , job
     , MAX(miles)
 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 70

Accepted Solution

by:
Scott Pletcher earned 1000 total points
ID: 39685589
I think you'll get best overall performance and ease of coding if you put the initial results into a table, then do the final query from that table.  For example:


declare @logbook table (Car varchar(50), LogDate DateTime, Job_no int, OdometerReadingEOJ int,
    PRIMARY KEY (Car, Job_no));
declare @logbook_day table (Id int IDENTITY(1, 1) NOT NULL, Car varchar(50) NOT NULL, LogDate datetime NOT NULL, Job_no int NOT NULL, OdometerReadingEOJ int NULL,
    PRIMARY KEY (Car, Id));

insert into @logbook values ('FORD', '20131121', 1, 67);
insert into @logbook values ('FORD', '20131121', 2, 239);
insert into @logbook values ('FORD', '20131121', 3, 500);
insert into @logbook values ('FORD', '20131122', 4, 700);
insert into @logbook values ('FORD', '20131122', 5, 1100);

insert into @logbook_day ( Car, LogDate, OdometerReadingEOJ, Job_no )
select t1.Car
     , t1.LogDate
     , MAX(OdometerReadingEOJ) AS OdometerReadingEOJ
     , MAX(Job_no) AS Job_no
 from @logbook t1
 group by t1.Car, t1.LogDate
 order by t1.Car, t1.LogDate
 
 --select * from @logbook_day
 
select ld.Car
     , ld.LogDate
     , ld.OdometerReadingEOJ - ISNULL((select OdometerReadingEOJ from @logbook_day ld2 where ld2.Car = ld.Car and ld2.Id = ld.Id - 1), 0) AS [TOTAL MILES]
     , ld.Job_no AS [JOB]
     , ld.OdometerReadingEOJ - (SELECT MAX(OdometerReadingEOJ) from @logbook l where l.Car = ld.Car and l.Job_no < ld.Job_no) AS [MAX MILES]
from @logbook_day ld
order by ld.Car, ld.LogDate
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

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

     When we have to pass multiple rows of data to SQL Server, the developers either have to send one row at a time or come up with other workarounds to meet requirements like using XML to pass data, which is complex and tedious to use. There is a …
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

750 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