count Query in sql

和自甴很熟 提交于 2020-01-16 18:22:06

问题


i have an issue with query

1st table (Master) Name :MainCategory with fields (Category_id,Category_name)

2nd Table (Transation) Name : Incident with fields (Incident_id,Category_id,subject,description)

i want to query to count the appearance of category_id in the table Transation

for eg result can be

Category_name   Number_of_Faults

Hardware          10
Software          22
Network           17

thanks

Kumar


回答1:


Try this:

SELECT a.Category_Name, COUNT(b.Incident_Id) Number_of_Faults
FROM MainCategory a JOIN Incident b
ON a.Category_id = b.Category_id
GROUP BY a.Category_Name



回答2:


Try this. You need a LEFT JOIN to deal with "no incidents" for a given category

SELECT
    M.Category_Name,
    COUNT(I.Category_id) AS Number_of_Faults
FROM
    MainCategory M
    LEFT JOIN
    Incident I ON M.Category_id = I.Category_id
GROUP BY
    M.Category_name


来源:https://stackoverflow.com/questions/5974636/count-query-in-sql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!