Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting count of records in child table using select statement

Tags:

I have a stored procedure in which i am trying to select all the columns of a table Table 1. There is another table which uses Table1 primary key as foreign key. I want to count number of records in this foreign key table with that select like this:

SELECT *, count(*) VacancyCount     FROM Table1 hc     LEFT JOIN Table2 hv     on hc.CompanyID = hv.CompanyID       WHERE hc.Deleted = 0     group by hc.CompanyID     ORDER BY NameLang1 

but it gives error:

Column 'dbo.Table1.NameLang1' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

Please suggest how to fix this?

like image 341
DotnetSparrow Avatar asked May 15 '13 08:05

DotnetSparrow


People also ask

How do I get a count in SELECT query?

You can use the SQL SELECT statement with the COUNT() function to select and display the count of rows in a table of a database.

How do I count the number of rows from a SELECT statement in SQL?

In SQL, you can make a database query and use the COUNT function to get the number of rows for a particular group in the table. Here is the basic syntax: SELECT COUNT(column_name) FROM table_name; COUNT(column_name) will not include NULL values as part of the count.

How can we get count of the number of records in a table?

Counting all of the Rows in a Table. To counts all of the rows in a table, whether they contain NULL values or not, use COUNT(*). That form of the COUNT() function basically returns the number of rows in a result set returned by a SELECT statement.

How do I count the number of records in a table in SQL?

Use the COUNT aggregate function to count the number of rows in a table. This function takes the name of the column as its argument (e.g., id ) and returns the number of rows for this particular column in the table (e.g., 5).


2 Answers

Please try:

select      *,     (select COUNT(*) from Table2 hv where hv.CompanyID=hc.CompanyID) VacancyCount from Table1 hc where     hc.Deleted = 0 order by hc.NameLang1, VacancyCount desc 

for ordering using the new column

select * from(     select          *,         CONVERT(NVARCHAR(100), (select COUNT(*) from Table2 hv where hv.CompanyID=hc.CompanyID)) VacancyCount     from Table1 hc     where         hc.Deleted = 0 )x Order by CASE WHEN @OrderByParam = 1 THEN NameLang1 ELSE VacancyCount END 

Provided column NameLang1 and VacancyCount are of same datatype.

like image 151
TechDo Avatar answered Oct 12 '22 14:10

TechDo


You're doing grouping wrong. You need to use all the columns from Table 1 in SELECT instead of '*' and in GROUP BY clause as well.

Or you can try a different approach like this:

SELECT * FROM Table1 hc LEFT JOIN (SELECT CompanyID, COUNT(*) cnt FROM Table2 GROUP BY CompanyID) hv on hc.CompanyID = hv.CompanyID   WHERE hc.Deleted = 0 ORDER BY NameLang1 
like image 36
ZZa Avatar answered Oct 12 '22 14:10

ZZa