Click here to Skip to main content
15,891,976 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
SQL
select c.*,COUNT(p.empid) from empcdetails c inner join emppdetails p on p.empid=c.empid where empstatus ='project' group by p.empid order by MAX(c.empdoj)asc
Posted
Updated 28-Dec-12 6:17am
v2

1 solution

c.* is the problem here. As you are using Group By clause, you have to mention only specific column names in your select list that are in group by.

Try this instead.
SQL
select p.empid,COUNT(p.empid) 
from empcdetails c 
inner join emppdetails p 
on p.empid=c.empid 
where empstatus = 'project' 
group by p.empid 
order by MAX(c.empdoj)asc


Try this subquery version, if you want to have all columns


SQL
select c.* , A.myCount
from empcdetails  c
join 
(
	select p.empid,COUNT(p.empid) as myCount
	from empcdetails cc 
	inner join emppdetails p 
	on p.empid = cc.empid 
	group by p.empid 
) A
on A.empid = c.empid
where empstatus = 'project' 
order by MAX(c.empdoj)asc



Hope this helps you.
 
Share this answer
 
v2
Comments
Zoltán Zörgő 28-Dec-12 13:25pm    
My 5! You are right, one has to list all non-aggregated fields in the group by clause.
[no name] 28-Dec-12 14:19pm    
Thanks :)
Member 9576671 28-Dec-12 22:11pm    
thanks but my requirement is displaying of all columns in emppdetails
[no name] 29-Dec-12 8:00am    
Then you have to use it in a sub query, otherwise you would not be able to apply Group By on your query.
[no name] 29-Dec-12 8:21am    
I have edited my answer above with the sub query version if you want to display all columns. Try that

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