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 に答える