从连接表中选择Distinct

时间:2015-02-28 21:42:09

标签: sql vb.net combobox distinct

我这里有两张表,夹具和报告。这两个都包含FixtureID字段。我试图创建一个函数,用我的组合框填充一个独特的FixtureID列表。每个单独的FixtureID代表多个ReportID(来自Report表)。

对于该Fixture中涉及的每个ReportID,我的第一次尝试使用所选侦察可用的每个FixtureID填充组合框。例如,如果为夹具制作了4个报告,则显示相同的夹具(显示报告日期)4次,我只需要显示一次。在这种情况下,我使用了我的Report类而不是Fixture,因为它显示了Report表中的ReportDate。

Report    |Fixture                       FixtureID = 1--> ReportID 1 
__________|________                                       ReportID 2 
FixtureID |FixtureID                                      ReportID 3 
ReportID  |HomeTeamID                                     ReportID 4                     
ScoutID   |AwayTeamID                    
ReportDate|                              FixtureID = 2--> ReportID 5
                                                          ReportID 6

在此示例中,组合框将显示1和2作为Fixture ID。

首次尝试:

'Filters reports based on the selected scout
        Public Function filterFixtureReports() As List(Of Report)
            'DISTINCT Report.FixtureID or Report.ReportDate not working
            Dim typeList As New List(Of Report)
            Dim Str As String = _
           <String> SELECT
                        * 
                    FROM 
                        Report 
                    WHERE
                        ScoutID = (<%= UC_Menu_Scout1.cmbScoutName.SelectedItem.ScoutID %>)
                    ORDER BY 
                        ReportDate      
           </String>
            Try
                Using conn As New SqlClient.SqlConnection(DBConnection)
                    conn.Open()
                    Using cmdQuery As New SqlClient.SqlCommand(Str, conn)
                        Using drResult As SqlClient.SqlDataReader = cmdQuery.ExecuteReader()
                            While drResult.Read
                                typeList.Add(New Report(drResult("ReportID"), drResult("ScoutID"), drResult("FixtureID"), drResult("PlayerID"), drResult("ReportDate")))
                            End While
                        End Using 'Automatically closes connection
                    End Using
                End Using

            Catch ex As Exception
                MsgBox("Report Exception: " & ex.Message & vbNewLine & Str)
            End Try

            Return typeList
        End Function

我的第二次尝试使用了Fixture类,但只返回零而不是单独的FixtureID。

第二次尝试:

Public Function filterFixtures() As List(Of Fixture)
        'DISTINCT Report.FixtureID or Report.ReportDate not working
        Dim typeList As New List(Of Fixture)
        Dim Str As String = _
       <String> SELECT
                    DISTINCT Fixture.FixtureID,
                    * 
                FROM 
                    Fixture
                INNER JOIN Report ON Report.FixtureID = Fixture.FixtureID
                WHERE
                    Report.ScoutID = (<%= UC_Menu_Scout1.cmbScoutName.SelectedItem.ScoutID %>)
                ORDER BY 
                    Report.ReportDate      
       </String>
        Try
            Using conn As New SqlClient.SqlConnection(DBConnection)
                conn.Open()
                Using cmdQuery As New SqlClient.SqlCommand(Str, conn)
                    Using drResult As SqlClient.SqlDataReader = cmdQuery.ExecuteReader()
                        While drResult.Read
                            typeList.Add(New Fixture(drResult("FixtureID")) With {
                                         .HomeTeamID = drResult("HomeTeamID"),
                                         .AwayTeamID = drResult("AwayTeamID")})
                        End While
                    End Using 'Automatically closes connection
                End Using
            End Using

        Catch ex As Exception
            MsgBox("Report Exception: " & ex.Message & vbNewLine & Str)
        End Try

        Return typeList
    End Function

2 个答案:

答案 0 :(得分:0)

高级流程

  1. 从第一个组合框中选择Scout。 (未在下面介绍。)
  2. 使用Scout Id生成FixtureIds列表
  3. 使用FixtureId和ScoutID识别正确的报告

  4. <强> 2。使用Scout Id生成FixtureIds列表 - 使用此选项填充第二个组合框。

    SELECT FixtureId, MAX(reportdate)
    FROM  Report
    WHERE ScoutID = (<%= UC_Menu_Scout1.cmbScoutName.SelectedItem.ScoutID %>)
    GROUP BY FixtureId
    

    第3。使用FixtureId和ScoutID识别正确的报告 - 使用此选项填充第3个组合框。

    SELECT *
    FROM  Report
    WHERE ScoutID = (<%= UC_Menu_Scout1.cmbScoutName.SelectedItem.ScoutID %>)
    AND FixtureID = <Fill in with 2nd combo box value>
    ORDER BY ReportDate
    
      

    注意:如果FixtureID只能与1个侦察兵相关联,那么   在上面的查询中不需要ScoutID条件。

答案 1 :(得分:0)

DISTINCT始终应用于所有选定的列。如果您希望1列不同,则只包含该列,例如:SELECT DISTINCT col FROM t1

另一种方法是使用GROUP BY,例如SELECT col FROM t1 GROUP BY col

使用group by您可以获得更多灵活性,因为您可以包含MIN()MAX()等聚合函数。

如果您想为其他列包含任意值,例如SELECT col1, MAX(col2) AS col2, MAX(col3) AS col3 FROM t1,这可能很有用。