Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have an access database with a particular column which contains the words "male" and "female", and would like to know the number of times each word reoccurs. and display the value in a label. thanks @all
Posted
Updated 15-Aug-14 22:00pm
v2

SQL
SELECT COUNT(CASE WHEN col='MALE' THEN 1 ELSE 0 END) AS Males, COUNT(CASE WHEN col='FEMALE' THEN 1 ELSE 0 END) AS Females FROM MyTable
 
Share this answer
 
Comments
kallymore 16-Aug-14 6:23am    
please note that i am a beginner please i need to know how to read the column and extract the number of times the word "male" is entered and the number of times the word "female" is entered in the column. e.g.
i have a database "Student_DB" with a table "tblstdnt" which contains a column "gender" the column gender contains words like "male" and "female" of like 100 rows. kindly help me please i need to submit this soonest thanks.
OriginalGriff 16-Aug-14 6:33am    
That is what that query *does* - it returns exactly that.
All you have to do is include that as the SELECT statement in your existing code...
kallymore 16-Aug-14 7:43am    
thanks for your answer i think i need to be spoonfed. still didnt get it
OriginalGriff 16-Aug-14 7:54am    
Start by running the query: either via a DataReader or a DataAdapter - whichever you are more familiar with.
You can then use the returned row as two columns - "Males" and "Females" - which contains the numbers.
Then just set the label.Text property:
myLabel.Text = string.Format("{0} males and {1} females", malesCount, femalesCount)
kallymore 16-Aug-14 10:23am    
thanks for your answer i think i need to be spoonfed. still didnt get it
Another approach
select count(col) male, 0 female from myTable where col = 'male'
union  
select 0 male, count(col) female from myTable where col = 'female
 
Share this answer
 
v2
Comments
kallymore 16-Aug-14 7:48am    
please am just a beginner in vb.net programming how do i display the result in a label and please does the word "col" mean that i should replace with corresponding column name?
select count(field_name) as malecount from myTable where filed_name = 'male'
 
Share this answer
 
thanks everyone
VB
<pre lang="vb"> 
Dim cn As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Kallymore-PC\Documents\Visual Studio 2012\Projects\count\count.accdb")
        Dim cmd As New OleDbCommand("select count (Gender) as i from userinfo where Gender = 'male'", cn)
        cn.Open()
        Dim reader As OleDbDataReader = cmd.ExecuteReader
        If reader.HasRows = True Then
        End If
        reader.Read()
        Label59.Text = reader(0).ToString()


this did the trick
 
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