Solved

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

Posted on 2014-11-14
1
138 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
Comment Utility
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
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.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

728 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

14 Experts available now in Live!

Get 1:1 Help Now