在此表中,当一个条目被输入到同一个表中时,它们可以与字段aref相关联,因为它们是相同的。
在2条记录中的一条记录中,bda始终是一个空白字段,但其他记录将始终包含bda。我需要用剩下的数据拉出这个bda
有人可以帮我把这个查询排序,这是我到目前为止的
SELECT ada,bda,sum(adur) / 60 as atime ,count(adur) as calls
FROM cr_cdr
WHERE script_res = 'sam0000'
AND adetdate BETWEEN CAST('2012-06-11 00:00:00 AM' AS datetime)
AND CAST('2012-06-11 11:59:59 PM' AS datetime)
GROUP BY ada,bda
答案 0 :(得分:0)
您可以尝试将表格加入自身。像这样:
SELECT c1.ada,c2.bda / 60 as atime ,count(c1.adur) as calls
FROM
cr_cdr c1
INNER JOIN
cr_cdr c2
ON( c1.aref = c2.aref )
WHERE
c2.bda IS NOT NULL
AND c1.script_res = 'sam0000'
AND c1.adetdate BETWEEN CAST('2012-06-11 00:00:00 AM' AS datetime)
AND CAST('2012-06-11 11:59:59 PM' AS datetime)
GROUP BY aref