我正在尝试在可定义的日期范围内搜索来自5个不同字段的问题代码,然后获取返回的每个问题代码的总和。下面的My Query对每个查询返回的每个问题代码进行求和,但不会将一个查询中的总计添加到另一个查询中。
SELECT CUSTRAC.PROBLEM_CO, Count(CUSTRAC.PROBLEM_CO) AS CountOfPROBLEM_CO1
FROM CUSTRAC
WHERE (((CUSTRAC.DATE_SHIPP) Between [Begin Date] And [End Date]))
GROUP BY CUSTRAC.PROBLEM_CO, ([custrac].[problem_co])
HAVING (((CUSTRAC.PROBLEM_CO)<>""));
Union SELECT CUSTRAC.PROB_2, Count(CUSTRAC.PROB_2) AS CountOfPROB_2
FROM CUSTRAC
WHERE (((CUSTRAC.DATE_SHIPP) Between [Begin Date] And [End Date]))
GROUP BY CUSTRAC.PROB_2, ([custrac].[prob_2])
HAVING (((CUSTRAC.PROB_2)<>""));
Union SELECT CUSTRAC.PROB_3, Count(CUSTRAC.PROB_3) AS CountofPROB_3
FROM CUSTRAC
WHERE (((CUSTRAC.DATE_SHIPP) Between [Begin Date] And [End Date]))
GROUP BY CUSTRAC.PROB_3, ([custrac].[prob_3])
HAVING (((CUSTRAC.PROB_3)<>""));
Union SELECT CUSTRAC.PROB_4, Count(CUSTRAC.PROB_4) AS CountofPROB_4
FROM CUSTRAC
WHERE (((CUSTRAC.DATE_SHIPP) Between [Begin Date] And [End Date]))
GROUP BY CUSTRAC.PROB_4, ([custrac].[prob_4])
HAVING (((CUSTRAC.PROB_4)<>""));
UNION SELECT CUSTRAC.PROB_5, Count(CUSTRAC.PROB_5) AS CountofPROB_5
FROM CUSTRAC
WHERE (((CUSTRAC.DATE_SHIPP) Between [Begin Date] And [End Date]))
GROUP BY CUSTRAC.PROB_5, ([custrac].[prob_5])
HAVING (((CUSTRAC.PROB_5)<>""));
答案 0 :(得分:0)
如果我了解您的要求,这应该可以满足您的需求:
SELECT DISTINCT A.ProbCode, SUM(A.ProbCount) AS ProbSum FROM
(
SELECT CUSTRAC.PROBLEM_CO AS ProbCode, Count(CUSTRAC.PROBLEM_CO) AS ProbCount
FROM CUSTRAC
WHERE CUSTRAC.DATE_SHIPP Between [Begin Date] AND [End Date] AND CUSTRAC.PROBLEM_CO <> ''
GROUP BY CUSTRAC.PROBLEM_CO
UNION ALL
SELECT CUSTRAC.PROB_2 AS ProbCode, Count(CUSTRAC.PROB_2) AS ProbCount
FROM CUSTRAC
WHERE CUSTRAC.DATE_SHIPP Between [Begin Date] AND [End Date] AND CUSTRAC.PROB_2 <> ''
GROUP BY CUSTRAC.PROB_2
UNION ALL
SELECT CUSTRAC.PROB_3 AS ProbCode, Count(CUSTRAC.PROB_3) AS ProbCount
FROM CUSTRAC
WHERE CUSTRAC.DATE_SHIPP Between [Begin Date] AND [End Date] AND CUSTRAC.PROB_3 <> ''
GROUP BY CUSTRAC.PROB_3
UNION ALL
SELECT CUSTRAC.PROB_4 AS ProbCode, Count(CUSTRAC.PROB_4) AS ProbCount
FROM CUSTRAC
WHERE CUSTRAC.DATE_SHIPP Between [Begin Date] AND [End Date] AND CUSTRAC.PROB_4 <> ''
GROUP BY CUSTRAC.PROB_4
UNION ALL
SELECT CUSTRAC.PROB_5 AS ProbCode, Count(CUSTRAC.PROB_5) AS ProbCount
FROM CUSTRAC
WHERE CUSTRAC.DATE_SHIPP Between [Begin Date] AND [End Date] AND CUSTRAC.PROB_5 <> ''
GROUP BY CUSTRAC.PROB_5
) AS A
GROUP BY ProbCode