从两个SQL查询的结果中查找总和

时间:2015-11-19 06:50:28

标签: mysql sql

declare @CURDATETIME DATETIME

SET @CURDATETIME = GETDATE()

Select
(select COUNT(TABLE1.RECID) AS Expr1
                FROM            TABLE1 LEFT OUTER JOIN
                                TABLE2 ON TABLE2.ISSUEID = TABLE1.ISSUEID
                WHERE           (TABLE1.STATUS = 3) AND (TABLE2.NOTIFICATIONINDEP = 1)) AS OpenNotifications,


            (Select 
                            count(TABLE1.RECID) AS [ar3]
                from            TABLE1 left outer join 
                        TABLE2 ON TABLE2.ISSUEID = TABLE1.ISSUEID 
                where   TABLE1.STATUS = 4 
                        and TABLE2.NOTIFICATIONINDEP = 1
                        AND dateadd(second, TABLE1.CREATEDTIME, TABLE1.createddate) <= dateadd(hour, -1, @CURDATETIME) 
                        and dateadd(second, TABLE1.FinishedTime, TABLE1.FinishedDate) between dateadd(hour, -2, @CURDATETIME) and @CURDATETIME) as AR3,
            (Select 
                            count(TABLE.RECID) as [ar4]
                from            TABLE1 left outer join 
                        TABLE2 ON TABLE2.ISSUEID = TABLE1.ISSUEID
                where   TABLE1.STATUS = 3 and TABLE2.NOTIFICATIONINDEP = 1
                        and dateadd(second, TABLE1.createdtime, TABLE1.createddate) < dateadd(hour, -1, getdate())) as AR4,     

1 个答案:

答案 0 :(得分:0)

你的问题不是很清楚,但正如你在评论中所说,你可以将下面两个选择状态的数量相加(假设你的select语句正在工作并返回整数值),并在查询中使用它。

select 
        (Select 
                        count(TABLE1.RECID) AS [ar3]
            from            TABLE1 left outer join 
                    TABLE2 ON TABLE2.ISSUEID = TABLE1.ISSUEID 
            where   TABLE1.STATUS = 4 
                    and TABLE2.NOTIFICATIONINDEP = 1
                    AND dateadd(second, TABLE1.CREATEDTIME, TABLE1.createddate) <= dateadd(hour, -1, @CURDATETIME) 
                    and dateadd(second, TABLE1.FinishedTime, TABLE1.FinishedDate) between dateadd(hour, -2, @CURDATETIME) and @CURDATETIME)
 +

        (Select 
                        count(TABLE.RECID) as [ar4]
            from            TABLE1 left outer join 
                    TABLE2 ON TABLE2.ISSUEID = TABLE1.ISSUEID
            where   TABLE1.STATUS = 3 and TABLE2.NOTIFICATIONINDEP = 1
                    and dateadd(second, TABLE1.createdtime, TABLE1.createddate) < dateadd(hour, -1, getdate()))

as total from dual