Solved

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

Posted on 2013-11-27
2
335 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 250 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 69

Accepted Solution

by:
Scott Pletcher earned 250 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

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

Suggested Solutions

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

803 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