我有下面的查询,我想要总结,在这一刻,我得到两行,每个查询的每个总和一个,无论如何都要添加两个总和所以返回总和的一行两个查询?提前谢谢
SELECT count(*)
FROM
eUser eUser2 RIGHT OUTER JOIN prcIncidentManagement ON (eUser2.eUserName=prcIncidentManagement.AssignedTo)
INNER JOIN eFolder ON (prcIncidentManagement.EFOLDERID=eFolder.eFolderID)
WHERE
eFolder.eCreationTime between '01/01/2015' and '13/10/2015'
union
SELECT count(*)
FROM
eUser RIGHT OUTER JOIN prcRequestFulfilment ON (prcRequestFulfilment.AssignedTo=eUser.eUserName)
INNER JOIN eFolder ON (eFolder.eFolderID=prcRequestFulfilment.EFOLDERID)
WHERE
eFolder.eCreationTime between '01/01/2015' and '13/10/2015'
答案 0 :(得分:1)
除非我建议您使用UNION ALL
代替UNION
,否则我们应该为您执行此操作。
如果您的两个查询都返回相同的值,则UNION
运算符会在两个值之间生成隐式DISTINCT
,而您的SUM
只会是其中一个值,而不是被加倍。
SELECT SUM(cnt_val) Sum_Values
FROM (
SELECT count(*) cnt_val
FROM eUser eUser2
RIGHT JOIN prcIncidentManagement
ON (eUser2.eUserName = prcIncidentManagement.AssignedTo)
INNER JOIN eFolder
ON (prcIncidentManagement.EFOLDERID = eFolder.eFolderID)
WHERE eFolder.eCreationTime BETWEEN '01/01/2015'
AND '13/10/2015'
UNION ALL
SELECT count(*) cnt_val
FROM eUser
RIGHT JOIN prcRequestFulfilment
ON (prcRequestFulfilment.AssignedTo = eUser.eUserName)
INNER JOIN eFolder
ON (eFolder.eFolderID = prcRequestFulfilment.EFOLDERID)
WHERE eFolder.eCreationTime BETWEEN '01/01/2015'
AND '13/10/2015'
) Result_Set
因此,假设两个查询的结果是:
2 --> first query result
2 --> second query result
如果您使用UNION
代替UNION ALL
,则总和将为2。
答案 1 :(得分:0)
这应该有效 -
SELECT SUM(Cnt) FROM
(SELECT count(*) AS Cnt
FROM
eUser eUser2 RIGHT OUTER JOIN prcIncidentManagement ON (eUser2.eUserName=prcIncidentManagement.AssignedTo)
INNER JOIN eFolder ON (prcIncidentManagement.EFOLDERID=eFolder.eFolderID)
WHERE
eFolder.eCreationTime between '01/01/2015' and '13/10/2015'
union
SELECT count(*)
FROM
eUser RIGHT OUTER JOIN prcRequestFulfilment ON (prcRequestFulfilment.AssignedTo=eUser.eUserName)
INNER JOIN eFolder ON (eFolder.eFolderID=prcRequestFulfilment.EFOLDERID)
WHERE
eFolder.eCreationTime between '01/01/2015' and '13/10/2015') A
答案 2 :(得分:0)
尝试:
select sum(cnt) total_cnt
from (
SELECT count(*) cnt
FROM eUser eUser2
RIGHT OUTER JOIN prcIncidentManagement ON eUser2.eUserName = prcIncidentManagement.AssignedTo
INNER JOIN eFolder ON prcIncidentManagement.EFOLDERID = eFolder.eFolderID
WHERE eFolder.eCreationTime between '01/01/2015' and '10/13/2015'
union all
SELECT count(*)
FROM eUser
RIGHT OUTER JOIN prcRequestFulfilment ON prcRequestFulfilment.AssignedTo = eUser.eUserName
INNER JOIN eFolder ON eFolder.eFolderID = prcRequestFulfilment.EFOLDERID
WHERE eFolder.eCreationTime between '01/01/2015' and '10/13/2015'
) x
您可以使用union all
代替union
如果您使用union
并且第一个和第二个查询返回的count
相同,则union
将删除一个计数,因为它只允许不同的值。
所以,你不会得到你期望的结果。
日期必须采用mm/dd/yyyy
格式,否则请使用convert
从字符串中获取数据。
答案 3 :(得分:0)
嗯,你可以取两个输出然后总和。无需使用Union all或Union。
Declare @SumOfIncident int= (SELECT count(*)
FROM eUser eUser2 RIGHT OUTER JOIN prcIncidentManagement ON (eUser2.eUserName=prcIncidentManagement.AssignedTo) INNER JOIN eFolder ON (prcIncidentManagement.EFOLDERID=eFolder.eFolderID)
WHERE eFolder.eCreationTime between '01/01/2015' and '13/10/2015' )
Declare @requestOfFullfillment int= (SELECT count(*)
FROM eUser eUser2 RIGHT OUTER JOIN prcIncidentManagement ON (eUser2.eUserName=prcIncidentManagement.AssignedTo) INNER JOIN eFolder ON (prcIncidentManagement.EFOLDERID=eFolder.eFolderID)
WHERE eFolder.eCreationTime between '01/01/2015' and '13/10/2015' )
SELECT @SumOfIncident + @requestOfFullfillment