Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How do I get the count of a field but only when the value of the field is greater than zero. For example, here is the code to get the count of the field regardless of its value:
Select Name,Count(Amount) From tableName Where ... Group By Name

However, I want to count only amount with values greater than 0.

Thanks
Posted
Updated 5-Jun-12 12:06pm
v2
Comments
Sergey Alexandrovich Kryukov 5-Jun-12 16:53pm    
What did you try so far? Any problems? :-)
--SA
Member 8317792 5-Jun-12 16:55pm    
The above code get all counts even if the value is 0. I want to get the count of only values greater than 0. PS I have many columns not just the amount that i want to get their count only if their values is greater than 0.

In SQL, if you want the count of a value when it is greater than zero, you would do it like so:

SQL
SELECT SUM(CASE WHEN Amount > 0 THEN 1 ELSE 0 END) AS AmountCount, Name
FROM tableName
GROUP BY Name


You would do the SUM statement for each column you wanted the count on. What it does is it only puts a one if the value meets your criteria (greater than zero). You could adapt this to meet other criteria for other columns too. For example, you could do a count of any value less than zero for a different column.

If you are using Microsoft Access to query the data, you can use the iif statement (yes, two i's) like so:

SQL
SELECT SUM(iif ([Amount] > 0, 1, 0) AS AmountCount, Name
FROM tableName
GROUP BY Name
 
Share this answer
 
v2
Comments
VJ Reddy 5-Jun-12 21:00pm    
Good answer. 5!
Member 8317792 6-Jun-12 12:30pm    
Does this work with OLEDB? anyway I will try it and let you know. Thanks
Tim Corey 6-Jun-12 12:35pm    
Sorry, I forgot to post Access-specific query syntax as well. I've updated my post with the info. I believe that is the correct syntax, although I cannot test it right now.
Member 8317792 6-Jun-12 12:50pm    
Unrecognized keyword WHEN. So it will not work with oledb :( Still searching for a solution for my problem...
Tim Corey 6-Jun-12 13:15pm    
Did you try my updated code for Access? It doesn't use WHEN.
SQL
Select [Name],Count(Amount) From tableName Where Amount>0 Group By [Name]
 
Share this answer
 
Comments
Member 8317792 6-Jun-12 12:31pm    
This will not work with my case since I have several columns as I said...
The Solution 1 given by Tim Corey is good.

In case you already have the DataTable read from data base, then LINQ can be used as follows:
C#
DataTable groupedData = new DataTable("GroupedData");
groupedData.Columns.Add("Name",typeof(string),null);
groupedData.Columns.Add("AmountCount",typeof(int),null);

tableName.AsEnumerable().GroupBy (d => d.Field<string>("Name")).Select( gr => {
    DataRow row = groupedData.NewRow();
    row["Name"] = gr.Key;
    row["AmountCount"] = gr.Count (g => g.Field<int>("Amount") > 0);
    return row;
}).CopyToDataTable(groupedData,LoadOption.OverwriteChanges);
 
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