我试图获得每次通话的最短时间。每个呼叫都可以有相应的呼叫ID记录的所有部分。我想要做的就是为每个调用id的最小时间做好准备,但是当我以一种方式查询它时,或者如果我以另一种方式查询它,我只获得最小值。
我想最终能够按小时计算所有来电
这是我一直在玩的查询,试图获取唯一的呼叫ID和时间,但这会返回当前重复的
例如:
Callid 1 = 2014-07-04 16:37:22.043
callid 2 = 2014-07-04 16:37:23.370
我想要的只是来自被叫1
的值select t.callid, (select min(timein) from loggeddata t2 where t2.callid = t.callid and t2.timein > t.timein
) as 'mintime'
from loggeddata t
答案 0 :(得分:2)
您不需要子查询,只需使用GROUP BY
定义分组表达式并将聚合添加为列:
select t.callid,
min(timein) as 'mintime'
from loggeddata
GROUP BY callid
答案 1 :(得分:0)
select callid,min(timein) from loggedata group by callid;
答案 2 :(得分:0)
;WITH CTE AS
(
select t.callid, (select min(timein) from loggeddata t2 where t2.callid = t.callid and t2.timein > t.timein
) as 'mintime'
from loggeddata t
)
Select c.callid,c.mintime from (select
row_number() over (partition by mintime order by callid desc) as seqnum
from CTE )c
where seqnum = 1