Results 1 to 3 of 3

Thread: SQL select statement difficulty

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2018
    Posts
    14

    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.

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    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

  3. #3
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,196

    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.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width