我有一张包含大量数据的表格。我正在存储rest call的记录详细信息,其中sentTime
和三个字段的组合(COL1,COL2,COL3)是唯一的。
我需要为每次休息电话拨打最后一个电话。
例如,如果每次调用API1,API2和API3 10次,我的表中大约有30行。我需要所有3个API的最后一次调用,所以我将得到3行,每个API一个。
我正在使用以下查询:
SELECT tb.id
FROM Table1 (nolock) tb
INNER JOIN (
SELECT col1, col2, col3, MAX(sentTime) as lastSentTime
FROM Table1 (nolock) GROUP BY col1, col2, col3) a
ON a.col1 = tb.col1 AND
a.col2 = tb.col2 AND
a.col2 = tb.col2 AND
a.lastSentTime = tb.sentTime
但它没有按预期工作。
例如:
id Name Sent_Time Temp_id Temp_id2
1 Delete 04/03/16 17:54 AB 2222701
2 Update 04/03/16 17:54 UD 6900001
3 Create 04/03/16 17:54 EL 2017301
4 Read 04/03/16 17:54 AB 2670001
5 Update 08/03/16 17:54 UD 1069501
6 Create 08/03/16 17:54 EL 3490801
除了数百万行外。 name,Temp_id和Temp_id2的组合是唯一的。
在java中,我已经获取了所有数据,并将其放入HashMap,密钥为name + Temp_id + Temp_id2
。所以它是独一无二的。我可以通过查询获得相同的数据吗?
答案 0 :(得分:0)
你可以试试这个。我想每个不同的REST调用都有自己的col1
,col2
和col3
值,你想要最新的。
SELECT MAX(sentTime) mostrecent_sentTime,
col1, col2, col3
FROM Table1
GROUP BY col1, col2, col3
答案 1 :(得分:0)
如果您想要行中的所有列,请使用窗口函数:
select t.*
from (select t.*,
row_number() over (partition by col1, col2, col3 order by sentTime desc) as seqnum
from t
) t
where seqnum = 1;
如果你的表只有四列(或者你只关心四列),那么@OllieJones建议的聚合可能更合理。