过滤TOP x最多发生在date1和date2之间

时间:2016-07-05 03:38:39

标签: vb.net

请参阅此问题:Visual Basic, filter on most occuring in Acces Databse

所以经过长时间的研究,我已经制作了一个表格并想出了如何在我的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

接下来我需要一些帮助,除了我如何过滤已经使用日期(01-07-16)以获得最多发生在date1和date2之外,我该怎么办?如果有人可以给我一个温和的推动方向,那将是一种善意。

更新#1

我从互联网上获取的信息中调查了WHERE和BETWEEN这一条款。我想出了这段代码:

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

1 个答案:

答案 0 :(得分:0)

我一如既往地为自己的问题提供自己的答案。

所以我需要在WHERE creation_date BETWEEN #7/2/16# AND #7/5/16#

之后加AS a ON notations.categorie = a.categorie

现在它按预期工作。

感谢jmcilhinney提供我需要的作品。