Solved

SQL Max Record

Posted on 2014-03-18
3
351 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
3 Comments
 
LVL 142

Expert Comment

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

Accepted Solution

by:
sdstuber earned 500 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 69

Expert Comment

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Sql query 34 22
sql calculate averages 18 32
Service Statictic 11 14
Error in query 3 0
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how the fundamental information of how to create a table.

743 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

12 Experts available now in Live!

Get 1:1 Help Now