Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 how to write the above query correctly

Author  Topic 

tsaliki
Starting Member

19 Posts

Posted - 2013-09-19 : 08:10:38
i have a table emp with records as follows:

empname highprtjobs

tsaliki h1
sasi h2
Srinivas h3
tsaliki h4
sasi h5


in this i wrote a select statement as follows:

select empname,count(highprtjobs)as noofhighprtjobs from testemp group by empname. i got the result as

empname noofhighprtjobs
sasi 2
srinivas 1
tsaliki 2


In this now i am trying to get the one which is having min noofhighprtjobs. So i wrote the above statement but it is throwing some error.So how do i get the empname whose noofhighprtjobs is minimum.




select empname,count(highprtjobs)as noofhighprtjobs from testemp group by empname
having min(count(highprtjobs))

VeeranjaneyuluAnnapureddy
Posting Yak Master

169 Posts

Posted - 2013-09-19 : 08:19:38
select empname,count(highprtjobs)as noofhighprtjobs from testemp
group by empname,highprtjobs

veeranjaneyulu
Go to Top of Page

tsaliki
Starting Member

19 Posts

Posted - 2013-09-19 : 08:22:13
No i want the result of only min one i.e here i want the result set of only srinivas
Go to Top of Page

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-09-19 : 08:41:58
[code]select TOP (1) empname,count(highprtjobs)as noofhighprtjobs from testemp group by empname
ORDER BY 2;[/code]
Go to Top of Page

VeeranjaneyuluAnnapureddy
Posting Yak Master

169 Posts

Posted - 2013-09-19 : 08:45:14
select empname,count(highprtjobs)as noofhighprtjobs from Temp1
group by empname
having count(highprtjobs) < 2

veeranjaneyulu
Go to Top of Page

sigmas
Posting Yak Master

172 Posts

Posted - 2013-09-19 : 19:37:40
select top 1 with ties emp, count(*) as cnt
from table_name
group by emp
order by cnt asc
Go to Top of Page
   

- Advertisement -