简体   繁体   English

在MS Access中进行查询

[英]Making a query in MS Access

I am having a problem with my query. 我的查询有问题。 I have 2 tables: 我有2张桌子:

Table 1 is AutoCompany it has fields company and CodeCar . 表1是AutoCompany它具有字段companyCodeCar CodeCar can be 3 or 4 depending on the type of car that company has. 根据公司拥有的汽车类型, CodeCar可以为3或4。

table 1: AutoCompany 表1:汽车公司

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

Table 2 CodeCarCompanies has the codes: 表2 CodeCarCompanies具有以下代码:

car | codeCar
mers    3
vW      4

I need to select the companies with the count of the occurance of the 2 codeCar s resulting in something like this: 我需要选择带有2 codeCar出现次数的公司,结果如下:

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

My attempt so far: 到目前为止我的尝试:

 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)

In MS Access, I think you want: 在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; 

The above was built using the MS Access query design window and the Sum Σ button 上面是使用MS Access查询设计窗口和Σ按钮构建的

Edit re Comment 编辑重新评论

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

Or 要么

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM