从数据表或字符串中获取不同的值?

时间:2012-08-02 16:09:51

标签: sql gridview datatable

我在下面创建了数据表,它在Page_Load

上提供GridView
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    Dim strQuery As String = "SELECT DISTINCT setsTbl.setName, trainingTbl.t_date, userAssessmentTbl.o_id FROM userAssessmentTbl LEFT JOIN trainingTbl ON userAssessmentTbl.tt_id = trainingTbl.tt_id LEFT JOIN setsTbl ON trainingTbl.s_id = setsTbl.s_id LEFT JOIN outcomesTbl ON userAssessmentTbl.o_id = outcomesTbl.o_id WHERE UserId = @UserId ORDER BY setName, t_date DESC "

    Dim cmd As New SqlCommand(strQuery)
    cmd.Parameters.AddWithValue("@UserId", userString)
    Dim dt As DataTable = GetData(cmd)

   GridView1.DataSource = dt
   GridView1.DataBind()

End Sub

目前它输出值:

SetName t_date o_id
name1 12.12.12 1
name2 13.07.12 1
name2 15.07.12 2
name3 16.07.12 4
name3 17.07.12 3

我想在下面输入我的DataTable来输出不同的行:

SetName t_date o_id
name1 12.12.12 1
name2 13.07.12 1
name3 16.07.12 4

任何人都可以告诉我这可以实现吗?

1 个答案:

答案 0 :(得分:0)

看起来你想要每个名字的最早日期。如果是这样,您可以使用Group By和min函数:

setsTbl.setName, Min(trainingTbl.t_date), Min(outcomesTbl.o_id)
FROM userAssessmentTbl LEFT JOIN trainingTbl ON userAssessmentTbl.tt_id = trainingTbl.tt_id 
LEFT JOIN setsTbl ON trainingTbl.s_id = setsTbl.s_id LEFT JOIN outcomesTbl 
ON userAssessmentTbl.o_id = outcomesTbl.o_id 
WHERE UserId = @UserId Group By setsTbl.setName ORDER BY setsTbl.setName