我有两张桌子A和B. A保存出站呼叫的数据。 (拨打时间和电话号码) B保存入站呼叫的数据。 (拨打接听时间和电话号码)。
我必须在拨打外拨电话后的7天内显示所有接收来自同一号码的入站呼叫的外拨电话的数据。如果我在7天内没有收到该号码的电话,则不应考虑该外拨电话。 EX:我拨打了#9999999999' 3月1日的号码,我收到了来自同一号码的入境电话' 9999999999' 3月5日。我必须在报告中显示此电话。
另一个需要考虑的方案是:如果我在3月1日和3月3日进行出站呼叫等,那么我必须考虑从出站呼叫开始的一周内首次拨出的呼出电话,并且仅在我有结果时显示该行军第一次打电话的入境电话。
我桌子上其中一个电话号码的数据如下:
表A:
9999999999 | 2015-12-23 xx:xx:xx
9999999999 | 2015-12-29 xx:xx:xx
9999999999 | 2015-12-30 xx:xx:xx
9999999999 | 2016-01-05 xx:xx:xx
9999999999 | 2016-01-06 xx:xx:xx
9999999999 | 2016-01-07 xx:xx:xx
9999999999 | 2016-01-07 xx:xx:xx
9999999999 | 2016-01-08 xx:xx:xx
9999999999 | 2016-01-19 xx:xx:xx
9999999999 | 2016-01-21 xx:xx:xx
9999999999 | 2016-01-22 xx:xx:xx
9999999999 | 2016-01-26 xx:xx:xx
9999999999 | 2016-01-28 xx:xx:xx
9999999999 | 2016-01-29 xx:xx:xx
9999999999 | 2016-02-02 xx:xx:xx
9999999999 | 2016-02-03 xx:xx:xx
9999999999 | 2016-02-04 xx:xx:xx
9999999999 | 2016-02-09 xx:xx:xx
9999999999 | 2016-02-11 xx:xx:xx
9999999999 | 2016-02-12 xx:xx:xx
表B:
9999999999 | 2016-01-08 xx:xx:xx
9999999999 | 2016-01-13 xx:xx:xx
9999999999 | 2016-01-19 xx:xx:xx
9999999999 | 2016-01-31 xx:xx:xx
9999999999 | 2016-02-11 xx:xx:xx
9999999999 | 2016-02-12 xx:xx:xx
我的输出应该是:
9999999999 | 2016-01-05 xx:xx:xx | 2016-01-08 xx:xx:xx
9999999999 | 2016-01-19 xx:xx:xx | 2016-01-19 xx:xx:xx
9999999999 | 2016-01-26 xx:xx:xx | 2016-01-31 xx:xx:xx
9999999999 | 2016-02-04 xx:xx:xx | 2016-02-11 xx:xx:xx
我的查询是:
SELECT a.phonenumber, Min(a.outboundcall), Min(b.inboundcall)
FROM TableA a
join TableB b
ON (a.phonenumber=b.phonenumber)
WHERE (a.outboundcall < b.inboundcall)
AND (DATEDIFF(MI, a.outboundcall,b.inboundcall) between 0 AND 10800)
group by a.phonenumber,
DATEADD(WEEK, DATEDIFF(WEEK, '19000101', a.outboundcall)/7*7, '19000101'),
DATEADD(WEEK, DATEDIFF(WEEK, '19000101', b.inbouncall)/7*7, '19000101')
ORDER BY 1,2,3
还应该考虑时间。 任何人都可以帮我写一个查询。
答案 0 :(得分:0)
<img src="Images/Optimized.png" class="img-responsive center-block parallax" style="margin-top:250px"/>