Click here to Skip to main content
15,898,134 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello All,

I have the following table in the DB,

ID Names
1 Sam
2 Khan
3 Julia
4 Khan
5 Wilson
6 Angeli
7 Maria
8 Khan
9 Sam
10 Sam
11 Julia
12 Maria

Now the problem is that I need a SQL Server query which may retrieve Names Group by the Names with their frequencies(how many times a Name appears)in the DESCending order. For example for the above table the output should be like this:

Names Frequency
Sam 3
Khan 3
Julia 2
Maria 2
Wilson 1
Angeli 1

I have tried both GroupBy and OrderBy Clauses in query but failed to get the desired results. Please help me out of this.

Thank you in advance.

Tipu
Posted

SQL
; WITH ALL_NAME
     AS (SELECT names        AS NAMES,
                COUNT(names) AS FREQUENCY
         FROM   test --YOUR_TABLE
         GROUP  BY names)
SELECT *
FROM   ALL_NAME
ORDER  BY FREQUENCY DESC
 
Share this answer
 
SQL
select name ,count(*) as frequency from testing group by name order by frequency desc


live demonstration
 
Share this answer
 
v3
Comments
tipu khan jan 3-Apr-14 15:00pm    
Thanks for yours response, but sorry I missed one another field, which is the DepartmentID. Now If I want to get the same results for a Specific DepartmentID I have to put the where clause, but it gives error. Can u please help me where to put the "where" cluase(Where DepartmentID = @DeptID)?
King Fisher 4-Apr-14 0:11am    
select name ,count(*) as frequency from testing Where DepartmentID = @DeptID group by name order by frequency desc
tipu khan jan 4-Apr-14 7:31am    
Thank you for your help, but the problem is still there .. It gives "no value given for one or more required parameters" exception, although m passing the parameter correctly .. can you please have a look at my code..

Here is my code ..

OleDbCommand CMD = new OleDbCommand();
OleDbDataAdapter DA = new OleDbDataAdapter();
CMD.Connection = Con;
CMD.CommandText = "select Names, COUNT(Names) as Frequency from Employees where DepartmentID = @DeptID Group By Names ORDER BY Frequency Desc";
CMD.Parameters.Add("@DeptID ", OleDbType.BigInt).Value = Convert.ToInt64(DeptID);
DA.SelectCommand = CMD;
DA.Fill(dt);
King Fisher 4-Apr-14 8:02am    
error line?.check with breakpoint
King Fisher 4-Apr-14 8:05am    
check with this :

CMD.CommandText = "select Names, COUNT(Names) as Frequency from Employees where DepartmentID ='"+DeptID+"' Group By Names ORDER BY Frequency Desc";


getting any error?
Table_1
Col1
A
A
A
B
B

select col1,count(col1) from table_1 group by col1

It gives me result like

A 3
B 2
 
Share this answer
 
Here is the solution. This is tested.

SQL
create table test(
id int identity(1, 1), names nvarchar(80)
)

insert into  test values ('Sam')
insert into  test values ('Khan')
insert into  test values('Julia')
insert into  test values ('Khan')
insert into  test values ('Wilson')
insert into  test values ('Angeli ')
insert into  test values ('Maria')
insert into  test values ('Khan')
insert into  test values ('Sam')
insert into  test values ('Sam')
insert into  test values ('Julia')
insert into  test values ('Maria')




SQL
 SELECT names, 
         COUNT(names) TotalCount
    FROM test
GROUP BY names
  HAVING COUNT(names) > 1
 
Share this answer
 

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