如何计算表中按类别划分的行数 [英] How Can I Count The Number Of Rows By Category In A Table
问题描述
我正在使用MS Access 2007.
在桌面1
1 ID
2项目
3块
4批
在桌面2
1 ID
2项目
3计数
它会变成这样
表1
ID项目块很多
1 FOREST 1 1
2 HOUSE 1 5
3 FOREST 5 8
4 FOREST 4 6
5 HOUSE 4 8
table 2
ID项目计数
1 FOREST 3
2 HOUSE 2
i想知道table1上的每个项目有多少然后它会显示在table2 row3
这是我的代码:
I'm using MS Access 2007.
On table1
1 ID
2 Project
3 Block
4 Lot
On table2
1 ID
2 Project
3 Count
It will become like this
table 1
ID PROJECT BLOCK LOT
1 FOREST 1 1
2 HOUSE 1 5
3 FOREST 5 8
4 FOREST 4 6
5 HOUSE 4 8
table 2
ID PROJECT COUNT
1 FOREST 3
2 HOUSE 2
i want to know how many per project on table1 then it will show on table2 row3
this is my code:
Try
Dim cmd As OleDbCommand = New OleDbCommand("SELECT (Select COUNT(PROJECT) FROM table1 WHERE PROJECT=FOREST as Project12Count)", con)
opendatabase()
Dim myDA As OleDbDataAdapter = New OleDbDataAdapter(cmd)
Dim myDataSet As DataSet = New DataSet()
myDA.Fill(myDataSet, "MyTable95")
DataGridView1.DataSource = myDataSet.Tables("MyTable95").DefaultView
closedatabase()
Catch ex As Exception
MessageBox.Show(ex.ToString)
Finally
closedatabase()
End Try
我曾尝试搜索许多可以使用的代码帮助我,但我找不到任何可以解决我的问题。
感谢任何可以帮助我的人。
Ive tried searching many codes that can help me but i cant find anything that can solve my problem.
Thanks to anyone who can help me.
推荐答案
试试这个:
Try this:
SELECT Project, COUNT(Project) FROM Table1
GROUP BY Project
应该给你想要的数字 - 但我可能不会将它存储在一个单独的表格中,因为它很容易与真实数字不同步。
That should give you the numbers you want - but I probably wouldn't store it in a separate table as it's far, far too easy for that to get out of synch which the "real" numbers.
这篇关于如何计算表中按类别划分的行数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!