我希望我能就如何最有效地处理正确有效的数据集做一些澄清。
以下是我正在使用的三个不同表格中的三个查询。 Donor_ID是表之间的关键,但正如您所看到的 - 每个Donor_ID都有多个记录关联 - runid_gmt列具有不同的日期。
理想情况下,我想为每条记录使用max(runid_gmt) - 并加入Donor_ID上的EMAIL和ADDRESSES表,但也只选择每个表中的max(runid_gmt)记录。
我相信这是我需要做的 - 但不确定。关于如何解决这个问题的任何建议?
SELECT donor_id, last_name, birthdate, runid_gmt
FROM [dbo].TBL_DONORS where donor_id = '51999441' order by runid_gmt desc;
SELECT donor_id, city, state, zip, runid_gmt
FROM [dbo].TBL_ADDRESSES where donor_id = '51999441' order by runid_gmt desc;
SELECT donor_id, donor_email, runid_gmt
FROM [dbo].TBL_EMAIL where donor_id = '51999441' order by runid_gmt desc;
答案 0 :(得分:1)
尝试row_number
窗口功能:
select * from
(select *, row_number() over(partition by donorid order by gmt desc) rn
from donors) t1 join
(select *, row_number() over(partition by donorid order by gmt desc) rn
from addresses) t2 on t1.donorid = t2.donorid join
(select *, row_number() over(partition by donorid order by gmt desc) rn
from emails) t3 on t1.donorid = t3.donorid
where t1.rn = 1 and t2.rn = 1 and t3.rn = 1