• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 254
  • Last Modified:

Selecting only one member_id for each member ib SQL

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
morinia
Asked:
morinia
  • 3
  • 3
  • 2
  • +3
1 Solution
 
Rajkumar GsSoftware EngineerCommented:
SELECT member_id, MAX(enrollment_date) FROM Table_A
GROUP BY member_id
0
 
Rajkumar GsSoftware EngineerCommented:
SELECT member_id, MAX(enrollment_date) enrollment_date FROM Table_A
GROUP BY member_id
0
 
moriniaAdvanced Analytics AnalystAuthor Commented:
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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
Rajkumar GsSoftware EngineerCommented:
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
 
moriniaAdvanced Analytics AnalystAuthor Commented:
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
 
OCDanCommented:
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
 
Anthony PerkinsCommented:
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
 
Deepak ChauhanSQL Server DBACommented:
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
 
Racim BOUDJAKDJIDatabase Architect - Dba - Data ScientistCommented:
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
 
Racim BOUDJAKDJIDatabase Architect - Dba - Data ScientistCommented:
@OCDan

Grouping on a single column is not the same as grouping on multiple columns.  The OP asks only for single column grouping.
0
 
moriniaAdvanced Analytics AnalystAuthor Commented:
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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 3
  • 3
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now