Solved

SQL Max Record

Posted on 2014-03-18
3
354 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 143

Expert Comment

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

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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

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.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

839 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