假定我们现在一个聊天记录要获取双方第一次互相发送信息间隔了多长时间
那个tbnn就是临时表的名字
select isnull(a.groupid,b.groupid) as groupid,a.createtime waiterTime,b.createtime clientTime,isnull(DATEDIFF(SS,a.createtime,b.createtime),-1) as second from (select groupid, min(createtime) as createtime from @tbnn where isjjr='1' group by groupid) a full join (select groupid, min(createtime)as createtime from @tbnn where isjjr='0' group by groupid) b on a.groupid=b.groupid
同样,这个方法也同样适用于分组求差
-- 假定资料表名叫:tbItems select isnull(a.名称, b.名称) as 名称, isnull(a.数量, 0) as 买入, isnull(b.数量, 0) as 卖出, isnull(a.数量, 0)-isnull(b.数量, 0) as 差价 from (select 名称, sum(数量) as 数量 from tbItems where 项目='买入' group by 名称) a full join (select 名称, sum(数量) as 数量 from tbItems where 项目='卖出' group by 名称) b on a.名称=b.名称
微信扫码查看本文
发表评论