1

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

表 1 是AutoCompany它有字段companyCodeCar. CodeCar可以是 3 或 4,具体取决于公司拥有的汽车类型。

表 1:汽车公司

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

表 2CodeCarCompanies有代码:

car | codeCar
mers    3
vW      4

我需要选择具有 2 codeCars 出现次数的公司,结果如下:

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

1 回答 1

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
于 2013-01-24T10:24:18.820 回答