我有一个链接的访问数据库,如下所示:
country year numTest
AF 1990 30
AF 1991 45
AF 1992 50
AF 1993 55
AF 1994 50
BZ 1990 409
BZ 1991 509
BZ 1992 405
BZ 1993 874
CO 1990 0
CO 1991 0
CO 1992 0
我创建的第一个查询是将这些年份汇总到每个国家(名为testPerformed)
SELECT TB_basicInfo.country, Sum(TB_hivTest.hivtest) AS SumOfhivtest
FROM TB_hivTest INNER JOIN TB_basicInfo ON TB_hivTest.ID = TB_basicInfo.ID
GROUP BY TB_basicInfo.country;
但是现在我想排除任何总和少于1的国家。所以我创建了一个新的查询如下:(名为testPerformed> 1)
SELECT testPerformed.country, testPerformed.SumOfhivtest
FROM testPerformed
WHERE ((testPerformed.SumOfhivtest)>1);
所以自然会排除CO
,如果我将每个查询分开运行,这将正常工作。但是有没有办法将它组合成一个查询。
我累了:
SELECT TB_basicInfo.country, Sum(TB_hivTest.hivtest) AS SumOfhivtest
FROM TB_hivTest INNER JOIN TB_basicInfo ON TB_hivTest.ID = TB_basicInfo.ID
GROUP BY TB_basicInfo.country
UNION
SELECT TB_basicInfo.country, SumOfhivtest
WHERE SumOfhivtest > 0;
但是访问不喜欢这个查询,因为它提示我输入SumOfhivtest
答案 0 :(得分:3)
如果我理解您要正确执行的操作,请尝试以下操作:
SELECT TB_basicInfo.country, Sum(TB_hivTest.hivtest) AS SumOfhivtest
FROM TB_hivTest INNER JOIN TB_basicInfo ON TB_hivTest.ID = TB_basicInfo.ID
GROUP BY TB_basicInfo.country
HAVING Sum(TB_hivTest.hivtest) > 0;