计算三个联合记录的记录

时间:2016-12-15 05:38:43

标签: sql ms-access join count

我在ACCESS中有以下表格

Table.KnowledgeArea | Table.CentralData | Table.AppointedHrs
--------------------+-------------------+-------------------
AreaKnowKey         | IdKey(key)        | ApointedHrsKey
AreaKnowDescr       | AreaKnowKey       | AppointedHrsDescr
                    | AppointedHrsKey   | 
                    | RFC               | 

如您所见,CentralData与KnowledgeArea和AppointedHrs

相关

我有以下SQL查询

SELECT KnowledgeArea.AreaKnowDescr, AppointedHrs.AppointedHrsDescr, CentralData.RFC
FROM AppointedHrs AS HRSNOMB, AppointedHrs 
INNER JOIN 
( KnowledgeArea INNER JOIN CentralData 
     ON KnowledgeArea.AreaKnowKey = CentralData.AreaKnowKey ) 
ON AppointedHrs.ApointedHrsKey = CentralData.ApointedHrsKey;

生成以下浏览表

AreaKnowDescr        | AppointedHrsDescr | RFC
---------------------+-------------------+-----------
Ingeniería Aplicada. | Asignatura        | CUES730901
Ingeniería Aplicada. | Asignatura        | CUES730901
Ingeniería Aplicada. | Asignatura        | CUES730901
Ingeniería Aplicada. | Asignatura        | CUES730901
Ingeniería Aplicada. | Asignatura        | CUES730901
Ingeniería Aplicada. | Asignatura        | CUES730901
Ciencias Básicas.    | Asignatura        | CUGM700216
Ciencias Básicas.    | Asignatura        | CUGM700216

但我想要像

这样的东西
KnowledgeArea       | AppointedHrsDescr | Number of RFC
--------------------+-------------------+--------------
Ingeniería Aplicada | Asignatura        | 10
Ingeniería Aplicada | Tiempo Parcial    | 10
Ingeniería Aplicada | Tiempo Completo   | 10
Ciencias básicas    | Asignatura        | 5
Ciencias básicas    | Tiempo Parcial    | 5
Ciencias básicas    | Tiempo Completo   | 5

我知道我必须使用计数功能。我有记录加入两张桌子,但我无法生产我想要的。

任何人都可以帮助我,获得适当的SQL语句???

非常感谢...

2 个答案:

答案 0 :(得分:0)

您已在此处列出了两次AppointedHrs表:

FROM AppointedHrs AS HRSNOMB, AppointedHrs 

逗号是交叉连接的过时语法,因此您将表中的每条记录与表中的每条记录组合在一起。这导致了那些重复。

为了计算每个区域和指定小时的RFC,您必须按这两个分组。

SELECT KnowledgeArea.AreaKnowDescr, AppointedHrs.AppointedHrsDescr, COUNT(*)
FROM AppointedHrs 
INNER JOIN 
( KnowledgeArea INNER JOIN CentralData 
     ON KnowledgeArea.AreaKnowKey = CentralData.AreaKnowKey ) 
ON AppointedHrs.ApointedHrsKey = CentralData.ApointedHrsKey
GROUP BY KnowledgeArea.AreaKnowDescr, AppointedHrs.AppointedHrsDescr
ORDER BY KnowledgeArea.AreaKnowDescr, AppointedHrs.AppointedHrsDescr;

如果可能存在重复说明(我不建议这样做),您必须将ID放在GROUP BY子句中,以便在结果中将它们分开。

答案 1 :(得分:0)

这不是答案。我只想展示编写相同查询的其他方法。

首先聚合然后才加入通常是一个好主意。在标准SQL中:

SELECT 
  ka.AreaKnowDescr,
  ah.AppointedHrsDescr,
  cd.RFCs
FROM 
(
  SELECT AreaKnowKey, ApointedHrsKey, COUNT(*) AS RFCs
  FROM CentralData
  GROUP BY AreaKnowKey, ApointedHrsKey
) cd
JOIN KnowledgeArea ka ON ka.AreaKnowKey = cd.AreaKnowKey
JOIN AppointedHrs ah ON ah.ApointedHrsKey = cd.ApointedHrsKey
ORDER BY ka.AreaKnowDescr, ah.AppointedHrsDescr;

在MS Access中,对于连接中括号的需要,它看起来有点笨拙且可读性较差。

以下是没有连接的相同内容:

SELECT 
  (
    SELECT ka.AreaKnowDescr
    FROM KnowledgeArea ka
    WHERE ka.AreaKnowKey = cd.AreaKnowKey
  ) AS AreaKnowDescr,
  (
    SELECT ah.AppointedHrsDescr
    FROM AppointedHrs ah
    WHERE ah.ApointedHrsKey = cd.ApointedHrsKey
  ) AS AppointedHrsDescr,
  COUNT(*) AS RFCs
FROM CentralData cd
GROUP BY AreaKnowKey, ApointedHrsKey
ORDER BY AreaKnowDescr, AppointedHrsDescr;