?
Solved

SQL Select distinct, where max day and year

Posted on 2015-02-04
2
Medium Priority
?
250 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
[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
2 Comments
 
LVL 24

Accepted Solution

by:
Phillip Burton earned 2000 total points
ID: 40588843
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
ID: 40589203
Perfect Thanks
0

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

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

In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
Viewers will learn how the fundamental information of how to create a table.
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

741 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