?
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
?
354 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 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

Fill in the form and get your FREE NFR key NOW!

Veeam® is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

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…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

719 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