1

我有两张桌子cdrcdr_01

////Cdr////
id calling_no called_no call_date duration
1 9899767876 0112356788 01-jan-2012 160
2 9999758743 0244356788 01-jan-2012 191

////cdr_01/////
id calling_no called_no call_date duration
1 9999758743 0244356788 01-jan-2012 190
2 9899767876 0112356788 01-jan-2012 163

我希望通过匹配每列而不是 id 来匹配两个表的输出,并且如果持续时间的差异为 -1, 0 , 1 ,那么将忽略要显示的其余部分。

输出 :

calling_no called_no call_date duration
9899767876 0112356788 01-jan-2012 163

请帮忙。

4

2 回答 2

1
SELECT c1.calling_no, 
       c1.called_no, 
       c1.call_date, duration =
       CASE
           WHEN c.duration > c1.duration THEN c.duration ELSE c1.duration
       END
  FROM cdr AS c INNER JOIN cdr1 AS c1 ON 
       c.calling_no = c1.calling_no AND 
       c.called_no = c1.called_no AND
       c.call_date = c1.call_date
 WHERE (SIGN(c.duration - c1.duration) * (c.duration - c1.duration)) > 1

编辑:更新查询考虑到cdr.duration可能是>cdr1.duration

于 2013-01-23T07:20:33.100 回答
0

这是可以得到你想要的结果的代码:

SELECT  
    c.calling_no ,  
    c.called_no ,
    c.call_date ,
    c.duration
FROM Cdr c
    JOIN Cdr_01 c1
    ON c.calling_no = c1.calling_no 
       AND c.called_no = c1.called_no 
       AND c.calle_date = c1.call_date
WHERE  (c.duration - c1.duration) NOT IN (-1, 0, 1) 
于 2013-01-23T07:22:07.917 回答