Solved

Selecting only one member_id for each member ib SQL

Posted on 2012-03-31
11
244 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
 
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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
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:
deepakChauhan 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
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…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

920 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

14 Experts available now in Live!

Get 1:1 Help Now