简体   繁体   中英

I have a sample table with distinct departments now the scenario is,need to display the highest repeated departments in a table with all columns

INPUT:

empid department  names    salary
----------------------------------
1   dotnet      klrahul      1000
2   dotnet      dhawan       2000
3   dotnet      virat        1500
4   design      dhoni        3000
5   design      karthik      2000
6   design      phant        3040
7   php         chawal       4000
8   php         kpandya      2000
9   php         skaual       4300
10  php         bhumra       2000
11  dotnet      vijay        1646

OUTPUT:

empid   department  names   salary
----------------------------------
1   dotnet      klrahul      1000
2   dotnet      dhawan       2000
3   dotnet      virat        1500
11  dotnet      vijay        1646
7   php         chawal       4000
8   php         kpandya      2000
9   php         skaual       4300
10  php         bhumra       2000

You can use a window function :

select top (1) with ties t.*
from (select t.*, count(*) over (partition by dept) as cnt
      from table t
     ) t
order by rank() over (order by cnt desc), dept;

please try this code

SELECT emp_id, dept,count(*) OVER (PARTITION by dept)
FROM table
ORDER BY count(*) OVER (PARTITION by dept) desc, 
emp_id

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM