I have a C# Windows app with an SQL table holding voter information. One of the stored fields is ‘Party’ with stored values of ‘Dem’, ‘Rep’, ‘Ind’, ‘Other’. I want to total by these values and use the count function to show total of all records. I know how to use count to get the total of all the records in the the record set but how do you count each value in the field.
I have a form with textboxes for Total Records, Total Dem, Total Rep, Total Ind and Total Other. There is a list box with radio buttons. The default button is ‘All’ showing all records. There is a radio button for each of the values (Dem, Rep, Ind and Other). When the radio buttons are selected the list is narrowed by the selection. The total in the textboxes does not change and always shows the total for each value.
Thanks in advance.
A straightforward way would be to build a scalar function for each count. Something like
And then a view that holds the counts for each party. There are more performant ways to do this but from your question it sounds like your just getting up off the ground and this would probably be the most straightforward way for you.
Learn about scalar functions here:
http://msdn.microsoft.com/en-us/library/ms186755.aspx
And views here:
http://msdn.microsoft.com/en-us/library/ms187956.aspx
A better way (for the database) would be to build a view that uses “group by” but it would return rows instead of columns which will create a little more work for you when setting the values to each of your c# controls.
http://msdn.microsoft.com/en-us/library/ms177673.aspx