我有以下查询:
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
返回此示例数据:
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
我想获取每个成员ID的最早日期,因此我的结果集如下所示:
MemberID Last Term End Date as a Trustee Last Name
1004 2008-06-30 Smith
答案 0 :(得分:5)
使用ROW_NUMBER()
标识每个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
答案 1 :(得分:1)
您可以使用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
答案 2 :(得分:-1)
这可能对性能更有效,需要考虑 -
假设第一个查询执行得很好,您可能希望将该结果集抛出到临时表中,然后运行查询将该数据分组到临时表中。如果需要,可以将索引添加到该临时表中。但是下面应该有效。
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]