2012-08-02 75 views
0

我創建低於提要的Page_Load從數據表或字符串中獲取不同的值?

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 

一個GridView目前它輸出值的數據表:

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 

我想獲得下面這樣的輸出不同行我的數據表:

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

有人可以告訴我這可以實現嗎?

+0

爲了澄清,你真的在​​爲每個'SetName'尋找第一個不同的行嗎? – 2012-08-02 16:13:14

+0

是的,這正是我所追求的 – theBo 2012-08-02 19:53:56

回答

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 
+0

如上所述,我所追求的是從上述代碼中獲得獨特setName的最佳方法(如示例所示)。通過sql select語句或過濾DataTable即可。 – theBo 2012-08-02 19:58:39