Solved

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

Posted on 2014-07-29
2
334 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
Comment Utility
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
Comment Utility
That worked wonderfully.  Thank you for such a quick response.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now