如何计算和创建这些计数的总和

时间:2014-10-03 16:37:38

标签: mysql sql

我需要帮助构建我的查询来计算系统类型ID。

示例:

SystemTypeID  Description           Acronym
1             System1 description   ABC
2             System2 description   DEF
3             System3 description   XYZ

我希望构建这样的东西:

Count  SystemTypeID  Description           Acronym
21     1             System1 description   ABC
100    2             System2 description   DEF
67     3             System3 description   XYZ

到目前为止,我知道如何做一个独特的,但我想要计算SystemTypeID的发生次数,然后将其分组:

select distinct(a.SystemTypeID), 
       b.Description, 
       b.Acronym
from operations.Systems a,
     operations.SystemTypes b  
where a.SystemTypeID=b.SystemTypeID 
order by a.SystemType_ID;

1 个答案:

答案 0 :(得分:1)

而不是DISTINCT,您需要的是COUNTGROUP BY

SELECT COUNT(a.SystemTypeID) AS `Count`, 
       a.SystemTypeID,
       b.Description, 
       b.Acronym
FROM operations.Systems a,
     operations.SystemTypes b  
WHERE a.SystemTypeID = b.SystemTypeID
GROUP BY a.SystemTypeID,
       b.Description, 
       b.Acronym
ORDER BY a.SystemType_ID;