Solved

SQL Max Record

Posted on 2014-03-18
3
352 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: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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to shrink a transaction log file down to a reasonable size.

863 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

23 Experts available now in Live!

Get 1:1 Help Now