生成查询时出现问题

时间:2013-01-23 06:53:37

标签: sql

我有两张表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

请帮忙。

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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)