Solved

MS SQL to select the row of data with the max value in a particular field

Posted on 2014-07-29
2
338 Views
Last Modified: 2014-07-29
Hello Experts!

I need some help with a query.  I have written a query that returns the data as anticipated.  However, now I need to modify it to provide only those records with the largest PID.INTERVAL_NUMBER when grouped by the PID.POLICY_ID.  For example, my query returns policy ID POL45379 three times, with interval numbers 1, 2, and 3.  I would like to only show the data associated with PID.INTERVAL_NUMBER 3.  I have reviewed several threads, but my attempts were fruitless.  Any help would be greatly appreciated.

Thanks,
J

Here is the current code:

SELECT PID.POLICY_ID, 
	PID.ITEM_NUMBER,
	PID.INTERVAL_NUMBER,
	PID.EFFECTIVE_DATE,
	PID.EXPIRATION_DATE,
	PD.STAT_LIMIT_OF_LIABILITY, 
	PD.EST_REPLACEMENT_COST, 
	PD.CLASS_OF_BUSINESS
FROM dbo.PR_POLICY_DESCRIPTION PD
INNER JOIN PR_ITEM_DESCRIPTION PID
ON PD.POLICY_ID=PID.POLICY_ID
WHERE ((PID.EFFECTIVE_DATE BETWEEN '2014-01-01' AND '2014-03-31') OR
		((PID.EFFECTIVE_DATE < '2014-01-01') AND (PID.EXPIRATION_DATE BETWEEN '2014-01-01' AND '2014-03-31')) OR
		((PID.EFFECTIVE_DATE < '2014-01-01') AND (PID.EXPIRATION_DATE >= '2014-03-31')))
		AND ITEM_NUMBER='1'
ORDER BY PID.POLICY_ID, PID.ITEM_NUMBER, PID.INTERVAL_NUMBER

Open in new window

0
Comment
Question by:ferguson_jerald
2 Comments
 
LVL 34

Accepted Solution

by:
Brian Crowe earned 500 total points
ID: 40227854
try this...

WITH cte
AS
(
	SELECT PID.POLICY_ID, 
		PID.ITEM_NUMBER,
		PID.INTERVAL_NUMBER,
		PID.EFFECTIVE_DATE,
		PID.EXPIRATION_DATE,
		PD.STAT_LIMIT_OF_LIABILITY, 
		PD.EST_REPLACEMENT_COST, 
		PD.CLASS_OF_BUSINESS,
		ROW_NUMBER() OVER (PARTITION BY PID.POLICY_ID ORDER BY PID.INTERVAL_NUMBER DESC) AS RowNumber
	FROM dbo.PR_POLICY_DESCRIPTION PD
	INNER JOIN PR_ITEM_DESCRIPTION PID
	ON PD.POLICY_ID=PID.POLICY_ID
	WHERE ((PID.EFFECTIVE_DATE BETWEEN '2014-01-01' AND '2014-03-31') OR
			((PID.EFFECTIVE_DATE < '2014-01-01') AND (PID.EXPIRATION_DATE BETWEEN '2014-01-01' AND '2014-03-31')) OR
			((PID.EFFECTIVE_DATE < '2014-01-01') AND (PID.EXPIRATION_DATE >= '2014-03-31')))
			AND ITEM_NUMBER='1'
)
SELECT *
FROM cte
WHERE RowNumber = 1
ORDER BY POLICY_ID, ITEM_NUMBER, INTERVAL_NUMBER

Open in new window

0
 

Author Closing Comment

by:ferguson_jerald
ID: 40227863
That worked wonderfully.  Thank you for such a quick response.
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

911 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

22 Experts available now in Live!

Get 1:1 Help Now