在MS Access中进行查询

时间:2013-01-24 10:12:26

标签: tsql ms-access access-vba

我的查询有问题。我有2张桌子:

表1为AutoCompany,其中包含字段companyCodeCarCodeCar可以是3或4,具体取决于公司的汽车类型。

表1:AutoCompany

company| CodeCar|  
  jora     3
  jora     4
  jora     3
 ghita     3
 ghita     3
 ghita     4
gheorghe   4
gheorghe   3
gheorghe   3

表2 CodeCarCompanies包含以下代码:

car | codeCar
mers    3
vW      4

我需要选择具有2 codeCar s出现次数的公司 结果是这样的:

   company  | MERS| VW
   jora        2     1
   ghita       2     1
   gheorghe    2     1

到目前为止我的尝试:

 SELECT     COUNT(dbo.AutoComany) AS MERS, dbo.Company, COUNT(dbo.AutoComany.     
 [CodeCar]) AS VW,  

   FROM   dbo.AutoComany FULL OUTER JOIN
                  dbo.AutoComany  ON dbo.АВТОМОБ.КодПредпр = AutoCompany.company
     WHERE     (dbo.CodeCarComapnies.[CodeCar] = 3)
    GROUP BY dbo..company, dbo.CodeCarComapnies.[CodeCar]
    HAVING      (dbo.CodeCarComapnies.[CodeCar] = 4)

1 个答案:

答案 0 :(得分:1)

在MS Access中,我认为你想要:

SELECT codecarcomapnies.car,
       Count(autocompany.codecar) AS CountOfCodeCar
FROM   autocompany
       INNER JOIN codecarcomapnies
               ON autocompany.codecar = codecarcomapnies.codecar
WHERE  autocompany.codecar  IN ( 3, 4 )
GROUP  BY codecarcomapnies.car; 

以上是使用MS Access查询设计窗口和Sum Σ按钮

构建的

编辑重新评论

SELECT Sum(IIf([autocompany].[codecar]=3,1,0)) AS mers, 
       Sum(IIf([autocompany].[codecar]=4,1,0)) AS vw
FROM autocompany

或者

TRANSFORM Count(autocompany.CodeCar) AS CountOfCodeCar
SELECT "Total" AS Total
FROM autocompany 
INNER JOIN CodeCarComapnies 
ON autocompany.CodeCar = CodeCarComapnies.codeCar
WHERE autocompany.CodeCar In (3,4)
GROUP BY "Total"
PIVOT CodeCarComapnies.car