I have the following query:
SELECT
cst_recno as [Member ID],
cmc_end_date as 'Last Term End date as a Trustee',
ind_first_name as 'Last Name',
ind_mid_name as 'First Name',
ind_last_name as 'Last Name',
cst_ixo_title_dn as 'title',
cst_org_name_dn as 'organization',
adr_country as 'Country',
adr_city as 'City',
adr_state as 'State',
cst_eml_address_dn as 'Email'
FROM mb_committee_x_customer
JOIN co_customer ON cst_key=cmc_cst_key
JOIN mb_committee ON cmt_key=cmc_cmt_key
JOIN co_individual ON ind_cst_key=cmc_cst_key
LEFT JOIN co_customer_x_address ON cmc_cxa_key=cxa_key
LEFT JOIN co_address ON cxa_adr_key = adr_key
WHERE cmc_end_date <= '9/6/2017'
AND ind_deceased_flag != 1
AND cmt_code = N'T'
ORDER BY cst_recno
returning this sample Data:
MemberID Last Term End Date as a Trustee Last Name
1004 2003-06-30 Smith
1004 2005-06-30 Smith
1004 2006-06-30 Smith
1004 2008-06-30 Smith
1004 2007-06-30 Smith
I would like to get the oldest date for each member id, so my result set looks like this:
MemberID Last Term End Date as a Trustee Last Name
1004 2008-06-30 Smith
Use ROW_NUMBER()
to identify the latest record for each memberID
:
SELECT t.*
FROM(SELECT ROW_NUMBER() OVER(PARTITION BY s.memberID ORDER BY cmc_end_date DESC) as rnk,
cst_recno as [Member ID],
cmc_end_date as 'Last Term End date as a Trustee',
ind_first_name as 'Last Name',
ind_mid_name as 'First Name',
ind_last_name as 'Last Name',
cst_ixo_title_dn as 'title',
cst_org_name_dn as 'organization',
adr_country as 'Country',
adr_city as 'City',
adr_state as 'State',
cst_eml_address_dn as 'Email'
FROM mb_committee_x_customer
JOIN co_customer ON cst_key=cmc_cst_key
JOIN mb_committee ON cmt_key=cmc_cmt_key
JOIN co_individual ON ind_cst_key=cmc_cst_key
LEFT JOIN co_customer_x_address ON cmc_cxa_key=cxa_key
LEFT JOIN co_address ON cxa_adr_key = adr_key
WHERE cmc_end_date <= '9/6/2017' AND ind_deceased_flag != 1 AND cmt_code = N'T') t
WHERE t.rnk = 1
You could use GROUP BY :
SELECT
cst_recno as [Member ID],
MAX(cmc_end_date) as 'Last Term End date as a Trustee',
ind_first_name as 'Last Name',
ind_mid_name as 'First Name',
ind_last_name as 'Last Name',
cst_ixo_title_dn as 'title',
cst_org_name_dn as 'organization',
adr_country as 'Country',
adr_city as 'City',
adr_state as 'State',
cst_eml_address_dn as 'Email'
FROM
mb_committee_x_customer
JOIN co_customer ON cst_key=cmc_cst_key
JOIN mb_committee ON cmt_key=cmc_cmt_key
JOIN co_individual ON ind_cst_key=cmc_cst_key
LEFT JOIN co_customer_x_address ON cmc_cxa_key=cxa_key
LEFT JOIN co_address ON cxa_adr_key = adr_key
WHERE cmc_end_date <= '9/6/2017' AND ind_deceased_flag != 1 AND cmt_code = N'T'
GROUP BY cst_recno, ind_first_name, ind_mid_name, ind_last_name, cst_ixo_title_dn,cst_org_name_dn,adr_country,adr_city,adr_state,cst_eml_address_dn
ORDER BY cst_recno
This may work better for performance, something to consider -
Assuming the first query performs nicely, you may want to throw that resultset into a temp table, then run a query grouping that data off of the temp table. Indexes can be added to that temp table if need be. But the below should work.
SELECT cst_recno as [Member ID],
cmc_end_date as 'Last Term End date as a Trustee',
ind_first_name as 'Last Name',
ind_mid_name as 'First Name',
ind_last_name as 'Last Name',
cst_ixo_title_dn as 'title',
cst_org_name_dn as 'organization',
adr_country as 'Country',
adr_city as 'City',
adr_state as 'State',
cst_eml_address_dn as 'Email'
INTO #tempTable
FROM mb_committee_x_customer
JOIN co_customer
ON cst_key=cmc_cst_key
JOIN mb_committee
ON cmt_key=cmc_cmt_key
JOIN co_individual
ON ind_cst_key=cmc_cst_key
LEFT JOIN co_customer_x_address
ON cmc_cxa_key=cxa_key
LEFT JOIN co_address
ON cxa_adr_key = adr_key
WHERE cmc_end_date <= '9/6/2017'
AND ind_deceased_flag != 1
AND cmt_code = N'T'
ORDER BY cst_recno
;WITH CTE ([Last Term End date as a Trustee], [Last Name])
AS
(SELECT MAX([Last Term End date as a Trustee]),
[Last Name]
FROM #tempTable
GROUP BY [Last Name])
SELECT tt.[Member ID],
c.[Last Term End date as a Trustee],
c.[Last Name]
FROM CTE c
JOIN #tempTable tt
ON c.[Last Term End date as a Trustee] = tt.[Last Term End date as a Trustee]
AND c.[Last Name] = tt.[Last Name]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.