Solved

Selecting only one member_id for each member ib SQL

Posted on 2012-03-31
11
249 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
  • 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
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 
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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

828 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