简体   繁体   中英

count and display number of rows based on field data

I am using a DB and one of the fields in my table is the name of a radio station by call letters. So i have multiple rows of data and each one has the field with a radio station affiliation. Some rows have the same radio station (so I might have 3 rows with Station X and 1 row with Station Y). How do I count the rows from that one table and then display the data with a count for each radio station. I know I need to loop through somehow but I am new to this. I am using VB.NET and SQL 2008.

In a table named Station,

SELECT DISTINCT Station.Name FROM Station

will return a list of distinct station names. You can iterate over this list using a foreach loop, substituting each station name in <Name> like this

SELECT COUNT(Station.Name) FROM Station WHERE Station.Name = '<Name>'

Assumed that you've declare the connection and fields name are station and affiliation ..

Dim ds As New DataSet
Dim da As OleDbDataAdapter   
dim tbl1 as New DataTable
dim tbl2 as New DataTable

da = New OleDbDataAdapter("SELECT * FROM station", connection)
da.Fill(tbl1)

For x As Integer = 0  to tbl1.Rows.Count-1
  da = New OleDbDataAdapter("SELECT * FROM affiliation WHERE station ='" & tbl1.Rows(x).Item("name") & "'", connection)
  tbl2=New DataTable
  da.Fill(tbl2)

  if tbl2.Rows.Count > 0 then MsgBox(format(tbl2.Rows.Count))
Next

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM