sql - Getting count of records in child table using select statement -
i have stored procedure in trying select columns of table table 1. there table uses table1 primary key foreign key. want count number of records in foreign key table select this:
select *, count(*) vacancycount table1 hc left join table2 hv on hc.companyid = hv.companyid hc.deleted = 0 group hc.companyid order namelang1 but gives error:
column 'dbo.table1.namelang1' invalid in select list because not contained in either aggregate function or group clause.
please suggest how fix this?
please try:
select *, (select count(*) table2 hv hv.companyid=hc.companyid) vacancycount table1 hc hc.deleted = 0 order hc.namelang1, vacancycount desc for ordering using new column
select * from( select *, convert(nvarchar(100), (select count(*) table2 hv hv.companyid=hc.companyid)) vacancycount table1 hc hc.deleted = 0 )x order case when @orderbyparam = 1 namelang1 else vacancycount end provided column namelang1 , vacancycount of same datatype.
Comments
Post a Comment