Click here to Skip to main content
15,917,454 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I've a SQL Compact server 3.5 database which contains multiple entries, each entry should classified with a certain type (For example: Criminal, Civil, Arbitration ... etc)
I want to populate a list or a table with all the classifications in the database (Criminal, Civil, Arbitration ... etc) (this is the easy part)

And to count all rows in database that should be under the same classification (namely, the row would have the classification name within it's data)

Simple Example:
http://i.imgur.com/6eMi8oW.jpg[^]
And the result should be:
http://i.imgur.com/swKFuof.jpg[^]

What I have tried:

The Working Code <3

C#
try
            {
                sqlcon.Open();
                SqlCeCommand cm = new SqlCeCommand("SELECT  AKtype,  COUNT(AKtype) AS AKcount FROM Aklashehat GROUP BY AKtype ORDER BY AKcount", sqlcon);
                SqlCeDataReader sqldr = cm.ExecuteReader();

                    while (sqldr.Read())
                    {
                        ListViewItem item = new ListViewItem((listView1.Items.Count + 1).ToString());
                        item.SubItems.Add(sqldr["AKtype"].ToString());
                        item.SubItems.Add(sqldr["AKcount"].ToString());
                        listView1.Items.Add(item);
                    }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, ex.Message);
            }
Posted
Updated 26-Jul-16 4:56am
v3
Comments
Richard Deeming 19-Jul-16 14:06pm    
So some combination of GROUP BY[^] and COUNT[^] then?

1 solution

This should do it:
SQL
SELECT
  Type,
  COUNT(Type)
FROM Aklashehat 
GROUP BY Type
ORDER BY COUNT(Type)

Some Background you can find -among lot other places- here:
SQL GROUP BY Statement[^]

Edit After feedback from OP Sherif Kamel - Professional Profile[^], SQL CE is Little bit different from e.g. MS SQL 2014
SQL
SELECT
  AKtype,
  COUNT(AKtype)  AS AKcount
FROM Aklashehat 
GROUP BY AKtype
ORDER BY AKcount
 
Share this answer
 
v3
Comments
Sherif Kamel 26-Jul-16 10:35am    
There's an error produced " Expressions in the GROUP BY list cannot contain aggregate function" ? Is this something related to SQL Compact Server or Am I doing something wrong in the listview. *Sorry for the late reply, but I was trying to figure it out on my own
[no name] 26-Jul-16 10:39am    
Strange, I really tested this in MS SQL Server 2014 and it runs well there.
Really not simply a typo?
Sherif Kamel 26-Jul-16 10:40am    
Well, I tried it with SQL CE cmd and It worked perfect, but I still can't get it to work in my application
[no name] 26-Jul-16 10:42am    
Can you post your SQL as comment here? Really the c# code part copy/paste :)
Sherif Kamel 26-Jul-16 10:43am    
SqlCeCommand cm = new SqlCeCommand("SELECT AKtype, COUNT(AKtype) FROM Aklashehat GROUP BY AKtype ORDER BY COUNT(AKtype)", sqlcon);

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