Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
Solved

# SQL Max Record

Posted on 2014-03-18
Medium Priority
359 Views
I have this query:
``````SELECT        pr.PartNum, pr.RevisionNum, pr.EffectiveDate, pr.PartNum + '*' + pr.RevisionNum AS PartNumRevisionNum
FROM            dbo.PartRev AS pr INNER JOIN
(SELECT        PartNum, MAX(EffectiveDate) AS maxeffdate
FROM            dbo.PartRev AS b
WHERE        (Approved = 1) AND (EffectiveDate <= GETDATE())
GROUP BY PartNum) AS b_1 ON b_1.PartNum = pr.PartNum AND b_1.maxeffdate = pr.EffectiveDate
WHERE        (pr.Approved = 1)
``````

It is returning:
1234 A 07-27-14
3456 B 08-12-14
4567 B 09-01-14
4567 C 09-01-14

I want it to return:
1234 A 07-27-14
3456 B 08-12-14
4567 C 09-01-14

How would I revise this to get this result.
0
Question by:maverick0728
[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

LVL 143

Expert Comment

ID: 39937187
0

LVL 74

Accepted Solution

sdstuber earned 2000 total points
ID: 39937189
SELECT partnum,
revisionnum,
effectivedate,
partnum + '*' + pr.revisionnum AS partnumrevisionnum
FROM (SELECT partnum,
revisionnum,
effectivedate,
ROW_NUMBER() OVER(PARTITION BY partnum ORDER BY effectivedate) AS rn
FROM dbo.partrev
WHERE approved = 1 AND effectivedate <= getdate()) as x
WHERE rn = 1
0

LVL 70

Expert Comment

ID: 39937262
SELECT        pr.PartNum, pr.RevisionNum, pr.EffectiveDate, pr.PartNum + '*' + pr.RevisionNum AS PartNumRevisionNum
FROM            dbo.PartRev AS pr INNER JOIN
(SELECT        PartNum, MIN(EffectiveDate) AS maxeffdate
FROM            dbo.PartRev AS b
WHERE        (Approved = 1) AND (EffectiveDate <= GETDATE())
GROUP BY PartNum) AS b_1 ON b_1.PartNum = pr.PartNum AND b_1.maxeffdate = pr.EffectiveDate
WHERE        (pr.Approved = 1)
0

## Featured Post

Question has a verified solution.

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

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
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 …
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
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.
###### Suggested Courses
Course of the Month11 days, 8 hours left to enroll