sql - Compared counted data from the same table -
i using microsoft sql server 2008 r2, , trying compare counted results same query ran following:
select e.jobtitle, count(p.businessentityid) [no. of persons] adventureworks2008.person.person p (nolock) join adventureworks2008.humanresources.employee e (nolock) on e.businessentityid = p.businessentityid group e.jobtitle
i following results expected:
jobtitle no. of persons accountant 2 accounts manager 1 accounts payable specialist 2 accounts receivable specialist 3 application specialist 4 assistant chief financial officer 1 benefits specialist 1 buyer 9 chief executive officer 1 chief financial officer 1 control specialist 2 database administrator 2 design engineer 3 document control assistant 2 document control manager 1 engineering manager 1
what want display job title , count these results count same, job title not same.
basically buyer not returned there no other group count of 9.
but assistant chief financial officer, benefits specialist among others returned there many job-titles count of 1.
what easiest , efficient way this? ahead.
you can cte:
with cte as(select e.jobtitle, count(p.businessentityid) [no. of persons] adventureworks2008.person.person p (nolock) join adventureworks2008.humanresources.employee e (nolock) on e.businessentityid = p.businessentityid group e.jobtitle) select * cte c1 exists(select * cte c2 c2.[no. of persons] = c1.[no. of persons] , c2.jobtitle <> c1.jobtitle)
Comments
Post a Comment