Solved

Selecting only one member_id for each member ib SQL

Posted on 2012-03-31
11
252 Views
Last Modified: 2012-04-02
Table_A has member_id and enrollment_date.  The file can have multiple enrollment dates for each member.

I would like to select the record with the most recent enrollment date for each member. I would like only one record for each member.
 
How can I achieve this is SQL.
0
Comment
Question by:morinia
[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
  • 3
  • 3
  • 2
  • +3
11 Comments
 
LVL 23

Expert Comment

by:Rajkumar Gs
ID: 37791160
SELECT member_id, MAX(enrollment_date) FROM Table_A
GROUP BY member_id
0
 
LVL 23

Expert Comment

by:Rajkumar Gs
ID: 37791161
SELECT member_id, MAX(enrollment_date) enrollment_date FROM Table_A
GROUP BY member_id
0
 

Author Comment

by:morinia
ID: 37791313
This works great for the two fields.  Is there a way to achieve the same results with multiple fields.
ie

member_id
last_name
first_name
enrollment_date
exit_date
0
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
LVL 23

Expert Comment

by:Rajkumar Gs
ID: 37791319
SELECT
member_id,
last_name,
first_name,
enrollment_date,
exit_date
FROM Table_A A
INNER JOIN
(
SELECT member_id, MAX(enrollment_date) enrollment_date FROM Table_A
GROUP BY member_id
) B on A.member_id = B.member_id AND A.enrollment_date = B.enrollment_date
0
 

Author Comment

by:morinia
ID: 37791532
I get an error on enrollmen_date (See Attached)

SELECT member_id, MAX(enrollment_date) enrollment_date FROM Table_A
GROUP BY member_id
) B on A.member_id = B.member_id AND A.enrollment_date = B.enrollment_date
Error-1.docx
0
 
LVL 9

Expert Comment

by:OCDan
ID: 37793202
Try this:

SELECT
member_id,
last_name,
first_name,
MAX(enrollment_date) 'Enrollment',
exit_date
FROM table_A
group by member_id, last_name, first_name, exit_date

If you have more than one exit date and want the max for that then use:
SELECT
member_id,
last_name,
first_name,
MAX(enrollment_date) 'Enrollment'
max(exit_date) 'Exit'
FROM table_A
group by member_id, last_name, first_name
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 37793501
I get an error on enrollmen_date (See Attached)
That is telling you that the column enrollment_date is not in Table_A.  So it may be best you post the schema to your table.
0
 
LVL 15

Accepted Solution

by:
Deepak Chauhan earned 250 total points
ID: 37793878
I think it will provide the expected result ,,,,,

select member_id,
last_name,
first_name,
enrollment_date,
exit_date from (
select row_number() over( partition by member_id order by enrollment_date desc
) as row,member_id,
last_name,
first_name,
enrollment_date,
exit_date
from table_A) a where row =1
0
 
LVL 23

Expert Comment

by:Racim BOUDJAKDJI
ID: 37793926
Try this...please no points...Assign points to RajkumarGS

SELECT 
member_id,
last_name,
first_name,
enrollment_date,
exit_date
FROM Table_A A
INNER JOIN
(
SELECT member_id, MAX(enrollment_date) as max_date FROM Table_A
GROUP BY member_id
) B on A.member_id = B.member_id AND A.enrollment_date = B.max_date

Open in new window

0
 
LVL 23

Expert Comment

by:Racim BOUDJAKDJI
ID: 37793928
@OCDan

Grouping on a single column is not the same as grouping on multiple columns.  The OP asks only for single column grouping.
0
 

Author Comment

by:morinia
ID: 37794130
deepakChauhan,

This does give the expected results, the problem I have now is there are multiple records with the same enrollment date, but the exit date is different.  It appears someone was enrolled and disenrolled on the same day and then reenrolled on the same day in another plan.


select member_id,
last_name,
first_name,
enrollment_date,
exit_date from (
select row_number() over( partition by member_id order by enrollment_date desc
) as row,member_id,
last_name,
first_name,
enrollment_date,
exit_date
from table_A) a where row =1
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
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…
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.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

615 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