Solved

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

Posted on 2014-07-29
2
352 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
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.

623 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