SQL select statement difficulty
What select command do I use to Select distinct groups from sql table and at the same time select count total contact numbers for those in each group and display the result in datagridview table?
The result should look like this in the datagridview:
Group | Total Contacts
group1 | 10
group2 | 24
group3 | 14
I know how the select command to use to populate the datagridview without the count but this looks confusing. I don’t know how to start with the select function.
Thank you.
Re: SQL select statement difficulty
If you're populating a datatable with your sql data, you can use the datatable's compute method to show the count of total contact numbers...
Label1.Text = [datatable name].Compute(SUM("field name"), Nothing).ToString
Re: SQL select statement difficulty
You could also do it with your SQL statement,
Code:
Using con As New OleDbConnection(My.Settings.waterConnectionString), da As New OleDbDataAdapter("Select crop, count(*) as cnt from groups group by crop", con),
dt As New DataTable
da.Fill(dt)
For Each row As DataRow In dt.Rows
Dim str As String = row("crop").ToString & " " & row("cnt").ToString
MessageBox.Show(str)
Next
End Using
I tested this on a table called "groups" and a field called "crop". You will need to use your own table and field names.