Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2014-07-29
2
Medium Priority
?
356 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 2000 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how the fundamental information of how to create a table.

824 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