0

我正在尝试从可定义的日期范围内的 5 个不同字段中搜索问题代码,然后获取返回的每个问题代码的总和。下面的“我的查询”将每个查询返回的每个问题代码相加,但不会将一个查询的总数添加到另一个查询。

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)<>""));
4

1 回答 1

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
于 2012-06-08T22:42:38.033 回答