Solved

Selecting only one member_id for each member ib SQL

Posted on 2012-03-31
11
242 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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

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.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
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.
Viewers will learn how the fundamental information of how to create a table.

743 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

12 Experts available now in Live!

Get 1:1 Help Now