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
3
Medium Priority
?
359 Views
Last Modified: 2014-03-18
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)

Open in new window


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
Comment
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
  • Learn & ask questions
3 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 39937187
0
 
LVL 74

Accepted Solution

by:
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

by:Scott Pletcher
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

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

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.

636 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