我有一个数据集,其中包括DATETIME列和BANKCASENUMBER列。我想为每个BANKCASENUMBER选择具有最新日期的行。
这是我尝试过的:
PROC SQL;
CREATE TABLE WORK.QUERY_FOR_WORK_QUERY_FOR_ACCOUNT AS
ORDER BY t1.DATETIME LIMIT 1
SELECT t1.LOGINNAME,
t1.FORENAME,
t1.SURNAME,
t1.BANKCASENUMBER,
MAX(t1.DATETIME),
t1.'Inbound/outbound'n,
t1.'succesvol?'n
FROM WORK.WORK_QUERY_FOR_ACCOUNTACTIVITIES t1
GROUP BY t1.BANKCASENUMBER;
QUIT;
对于每个BANKCASENUMBER,返回的表应将包含latset DATETIME的行显示为空白。因此,每个BANKCASENUMBER应该出现一次。
答案 0 :(得分:1)
我相信这就是您要寻找的。想法是在子查询中获取每个银行案例编号的最新时间戳,并将其加入到主表中。
PROC SQL;
CREATE TABLE WORK.QUERY_FOR_WORK_QUERY_FOR_ACCOUNT AS
SELECT t1.LOGINNAME,
t1.FORENAME,
t1.SURNAME,
t1.BANKCASENUMBER,
t1.DATETIME,
t1.'Inbound/outbound'n,
t1.'succesvol?'n
FROM WORK.WORK_QUERY_FOR_ACCOUNTACTIVITIES t1
JOIN (
SELECT t2.bankCaseNumber,
MAX(t2.dateTime) as maxDateTime
FROM WORK.WORK_QUERY_FOR_ACCOUNTACTIVITIES t2
GROUP BY t2.BANKCASENUMBER
) m
ON m.bankCaseNumber = t1.bankCaseNumber
AND t1.dateTime = m.maxDateTime
;
QUIT;
答案 1 :(得分:0)
这似乎有点简单。由于我们是按LOGINNAME分组的,having语句将引用每个组中的max(DATETIME)
proc sql noprint;
create table QUERY_FOR_WORK_QUERY_FOR_ACCOUNT as
select * from WORK_QUERY_FOR_ACCOUNTACTIVITIES
group by LOGINNAME
having DATETIME = max(DATETIME);
quit;