請參考這個問題:Visual Basic, filter on most occuring in Acces Databse過濾前X DATE1之間的大多數發生和date2
所以長期研究後,我已經做了一個表格,並計算出如何顯示在我的datagridview1查詢數據時,我做了像這樣(作爲一個私人小組)
Private Sub FilterNotations(ByVal Top As String)
Dim TopCat As String
TopCat = "TOP " & Top
con.Open()
Cmd = New OleDbCommand("SELECT " & TopCat & " categorie, COUNT(Id) AS n FROM notations GROUP BY categorie ORDER BY COUNT(Id) DESC", con)
Cmd.ExecuteNonQuery()
con.Close()
Using Cmd = New OleDbCommand("SELECT notations.* FROM notations INNER JOIN (SELECT " & TopCat & " categorie, COUNT(Id) AS n FROM notations GROUP BY categorie ORDER BY COUNT(Id) DESC) AS a ON notations.categorie = a.categorie", con)
con.Open()
Using Dad = New OleDbDataAdapter(Cmd)
Dst.Clear()
DataGridView1.DataSource = Dst.Tables()
Dad.Fill(Dst, "notations")
DataGridView1.DataSource = Dst.Tables(0)
End Using
End Using
con.Close()
End Sub
我需要一些幫助,接下來的事情我怎麼能,除了我如何篩選已經使用日期爲(16年1月7日),以獲得發生在date1和date2之間的最多。如果有人能給我一個溫順的方向,那就是善良的方向。
更新#1
我已經調查WHERE和之間,距離信息,我可以從互聯網上吸氣劑條款。我想出了這個代碼:
Private Sub FilterNotations(ByVal Top As String)
Dim TopCat As String
TopCat = "TOP " & Top
con.Open()
Cmd = New OleDbCommand("SELECT " & TopCat & " categorie, COUNT(Id) AS n FROM notations WHERE creation_date BETWEEN #7/2/16# AND #7/5/16# GROUP BY categorie ORDER BY COUNT(Id) DESC", con)
Cmd.ExecuteNonQuery()
con.Close()
Using Cmd = New OleDbCommand("SELECT notations.* FROM notations INNER JOIN (SELECT " & TopCat & " categorie, COUNT(Id) AS n FROM notations WHERE creation_date BETWEEN #7/2/16# AND #7/5/16# GROUP BY categorie ORDER BY COUNT(Id) DESC) AS a ON notations.categorie = a.categorie", con)
con.Open()
Using Dad = New OleDbDataAdapter(Cmd)
Dst.Clear()
DataGridView1.DataSource = Dst.Tables()
Dad.Fill(Dst, "notations")
DataGridView1.DataSource = Dst.Tables(0)
End Using
End Using
con.Close()
End Sub
問題是,在我的數據網格中,它不顯示我希望的結果。 SELECT TOP x函數效果很好,但不會過濾帶日期。 有人可以給我一個提示。
親切的問候Jordy
根本沒有進行過濾。如果你想用SQL查詢過濾數據,那麼必須有一個WHERE和/或HAVING條款。你既沒有過濾,也沒有過濾。爲了按日期進行過濾,您需要添加一個帶有與日期相關的條件的WHERE條款。我建議你對SQL WHERE條款做一些研究。 – jmcilhinney
對不起,我的意思是排序,但我需要做的是按日期過濾。我將調查Where子句。 –
@jmcilhinney - 在調查WHERE和BETWEEN子句後,我更新了我的原始問題,以解釋我在第一個代碼中所做的更改。 –