Solved

SQL Select distinct, where max day and year

Posted on 2015-02-04
2
182 Views
Last Modified: 2015-02-04
I have a table with these fields (for simplicity)

MemberNum(int),     NoteMonth (int),  NoteYear(int), Notes(varchar(max))
-----------------------------------------------------------------------------------------------------
100000,   1 , 2014, 'ABC'
100000,  2, 2014, 'CDE'
100000, 1, 2015, 'HEL'
200000, 12, 2013, 'AFG'
200000, 5, 2014,  'MMF'
200000, 7, 2015, 'PPP'



Every month a record is added with the year, and month

I need to get the latest Notes for each member


The Query Should Return the following rows

100000, 1, 2015, 'HEL'
200000, 7, 2015, 'PPP'
0
Comment
Question by:Ess Kay
2 Comments
 
LVL 24

Accepted Solution

by:
Phillip Burton earned 500 total points
Comment Utility
How about this:

With myTable as (
Select MemberNum, NoteMonth, NoteYear, Notes, 
Row_Number() Over(partition by MemberNum Order by NoteYear Desc, NoteMonth Desc) as MyRowNumber
From TableName)
Select MemberNum, NoteMonth, NoteYear, Notes
from myTable
Where MyRowNumber = 1

Open in new window

0
 
LVL 15

Author Closing Comment

by:Ess Kay
Comment Utility
Perfect Thanks
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Query Missing Money orders... 6 62
How to convert sql code to access query 8 53
Complex SQL 10 32
Test a query 23 11
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
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…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

744 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

17 Experts available now in Live!

Get 1:1 Help Now