Solved

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

Posted on 2013-11-27
2
336 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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Suggested Solutions

     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 …
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

820 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