Solved

SQL - How do I get rows with highest value in one column

Posted on 2014-11-14
1
142 Views
Last Modified: 2014-11-14
I have one table with all contracts for cars. One car can have more contracts. For example one contract from 1 januari 2013 until 1 july 2013 and a next contract from 1 sept 2013 until 2 febr 2014.
How can I make a view in SQL 2000 server that shows the last contract of each car.

Example:
number  -  carnumber  -  enddate
123          -  195               -  20130701
158          -  155              -   20141012
168          -  195               -  20140201

As result I want
number  -  carnumber  -  enddate
158          -  155              -   20141012
168          -  195               -  20140301
0
Comment
Question by:cleaclev
1 Comment
 
LVL 24

Accepted Solution

by:
Phillip Burton earned 500 total points
ID: 40442307
select (select number from mytable M1 where M.carnumber = M1.carnumber and max(M.enddate) = M1.enddate) as number, carnumber, max(enddate) as enddate
from mytable M
group by carnumber
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

772 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