日期:2014-05-18 浏览次数:20575 次
SELECT sessionid FROM EXTERNAL_CDR.dbo.CDRMAIN WHERE TalkDuration > 0 AND sessionid IN ( SELECT sessionid FROM EXTERNAL_CDR.dbo.CDRMAIN WHERE TalkDuration = 0 ) --这句的意思就是得到TalkDuration既大于0又有等于0的sessionid --等价于 SELECT sessionid FROM ( SELECT * FROM EXTERNAL_CDR.dbo.CDRMAIN WHERE TalkDuration > 0 UNION ALL SELECT * FROM EXTERNAL_CDR.dbo.CDRMAIN WHERE TalkDuration = 0 ) a GROUP BY sessionid HAVING ( COUNT(1) > 1 )
------解决方案--------------------
-- 把in 和 not in 都去掉 ;WITH m AS ( SELECT sessionid FROM ( SELECT * FROM EXTERNAL_CDR.dbo.CDRMAIN WHERE TalkDuration > 0 UNION ALL SELECT * FROM EXTERNAL_CDR.dbo.CDRMAIN WHERE TalkDuration = 0 ) a GROUP BY sessionid HAVING ( COUNT(1) > 1 ) ),m1 AS ( SELECT sessionid FROM EXTERNAL_CDR.dbo.CDRMAIN tic WHERE TalkDuration = 0 AND Direction = 1 AND EXISTS ( SELECT 1 FROM dbo.t_user_Users tuu WHERE EXISTS ( SELECT 1 FROM dbo.t_user_UserCombo uc LEFT JOIN dbo.t_user_Users uu ON uc.c_userID = uu.c_userID WHERE uu.c_state = 1 AND uc.c_comboID = '559' AND uc.c_beginTime <= GETDATE() AND uc.c_endTime > GETDATE() AND uc.c_state = 1 AND tuu.c_userID = uc.c_userID ) AND tic.CallerNum_bak = tuu.c_mobile ) AND starttime >= 1330531200 AND starttime < 1331049600 ) SELECT RingDuration , QueueDuration FROM EXTERNAL_CDR.dbo.CDRMAIN a LEFT JOIN m b ON a.sessionid = b.sessionid LEFT JOIN m1 c ON a.sessionid = c.sessionid WHERE b.sessionid IS NULL AND c.sessionid IS NOT NULL