Click here to Skip to main content
15,910,797 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to get a sql query working to count the number of items by their categories to display on a webpage. Any assistance would be appreciated

i want to count the Catagories with jobs

like
Sales&Marketing(5)
Accounts and Finance(2)


as so on.. my tables are
CataGories Table
CatID   CatName
1       Sales&Markting
2       Account and Finance
3       Web DEVELOPMENT
4       Media


and other table as Job
JobID  CatID  JOBtitle
  1 2 Sales Manager
  2       2    Seles Executive
  2       3    Java Developer


So plz tell me i m understand to get result
Posted
Updated 20-Sep-13 8:33am
v3

Try:
SQL
SELECT c.CatName, COUNT(j.JobID) FROM Job j 
JOIN Catagories c ON j.CatID=c.CatID 
GROUP BY C.CatName
 
Share this answer
 
v2
Comments
Maciej Los 20-Sep-13 14:38pm    
You were a bit quickest, Paul ;)
paracha1 20-Sep-13 14:42pm    
no its not working....... its will display the message
Column 'Catagories.CatName' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Try to use below query:
SQL
SELECT 
FROM (CatName + ' (' + CONVERT(VARCHAR(30),CountOfCtgs) + ')'
    SELECT C.CatName, COUNT(C.CatName) AS CountOfCtgs
    FROM Categories AS C INNER JOIN Job AS J ON C.CatID = J.CatID
    GROUP BY C.CatName
    ORDER BY C.CatName
) AS T
 
Share this answer
 
v2
Comments
Maciej Los 20-Sep-13 14:43pm    
You're welcome ;)
paracha1 21-Sep-13 2:56am    
Mr. Maciej Los;
----
SELECT C.CatName, COUNT(C.CatName)
FROM Categories AS C INNER JOIN Job AS J ON C.CatID = J.CatID
GROUP BY C.CatName -------

This query is executed Sucess but show only those record who jobs asigned catagories but other catagoreis will not show this query show error on asp.net page.

i want to show result like as Sales&Markting(2)HR(3)Account and Financ(4)

Information&Technology(0)Medical(0)Engineering(0)

and now tell me how is possibleplz send me the sultion as soon as possible

Thanks Maciej
Maciej Los 21-Sep-13 11:52am    
Have a look at the answer now ;)
paracha1 21-Sep-13 13:16pm    
i m hardly trying to get result but i m not able to get the result like as
Sales&Markting(2)HR(3)Account and Financ(4)

Information&Technology(0)Medical(0)Engineering(0)
but show only catagories will asigned job other catagories not show.i have 6 job using 4 catagoreis. just tell me plz show empty catagories show HR(0) Management(0) and so on...........plz tell me the right query. thanks u
Maciej Los 22-Sep-13 13:04pm    
Replace INNER JOIN with RIGHT JOIN and/or LEFT JOIN and see what happens ;)
i hardly trying and solove its problem
thanks to all

Quote:
SELECT c.CatName, COUNT(j.JobID) as Result FROM Job j
right JOIN CATAGORIES c ON j.CatID=c.CatID
GROUP BY C.CatName
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900