vb.net中的sql server distinct关键字

时间:2014-11-24 10:26:46

标签: sql-server vb.net datagridview

大家好我需要帮助从sql数据库获取数据到datagrid。 我想计算不同的值并将其放入datagrid列,这也是一个不同的值,如下所示: -

| Partyname | Turbo | Truck | Total |
+-----------+-------+-------+-------+
| Abc       | 33    | 3     | 36    |
| xyz       | 30    | 10    | 40    |
| qwe       | 2     | 4     | 6     |

但我得到的是这个!: -

| Partyname | Turbo | Truck | Total |
+-----------+-------+-------+-------+
| Abc       |       |       |       |
| xyz       |       |       |       |
| qwe       |       |       |       |

我的代码是: -

  Dim sCommand, cmd As SQLCommand
    Dim sadp As SQLDataAdapter
    Dim sds As New DataSet
    Dim stable As New DataTable
    Private Sub OK_Button_Click_1(sender As System.Object, e As System.EventArgs) Handles OK_Button.Click
        Dim sts As String = ""
        Dim dt, dt2 As String
        dt = (Date_from.Value.Date).ToShortDateString + " " + (DateTimeInput1.Text)
        dt2 = (Date_to.Value.Date).ToShortDateString + " " + (DateTimeInput2.Text)
        Try
            Credit_report_daily.Show()
            Dim sql As String
            sql = "SELECT Distinct Party_name FROM Weightment Where Cash_Cr='Credit' AND (Weigh_date Between @dt AND @dt2) Group by Party_name"
            Using com As New SqlCommand(sql, Con)
                Con.Open()
                com.Parameters.AddWithValue("@dt", DateTime.Parse(dt))
                com.Parameters.AddWithValue("@dt2", DateTime.Parse(dt2))
                sadp = New SqlDataAdapter(com)
                sds = New DataSet()
                sadp.Fill(sds, "General")
                stable = sds.Tables("General")
                Credit_report_daily.DataGridView1.DataSource = sds.Tables("General")
                Dim cmd As New SqlCommand
                cmd.Connection = Con
                cmd.CommandText = "SELECT Distinct Vehicle_Type FROM Weightment Where Cash_Cr='Credit' AND (Weigh_date Between @dt AND @dt2) Group by Vehicle_Type"
                cmd.Parameters.AddWithValue("@dt", DateTime.Parse(dt))
                cmd.Parameters.AddWithValue("@dt2", DateTime.Parse(dt2))
                Dim da As SqlDataReader = cmd.ExecuteReader
                While da.Read
                    Credit_report_daily.DataGridView1.Columns.Add(da.Item(0), da.Item(0))
                End While
                da.Close()
                Con.Close()
            End Using
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
        Me.Close()
    End Sub

你能帮我吗??????

通过计算Column Vehicle_type中有多少个不同的值来获取列 像这样: -

                    Dim cmd As New SqlCommand
                    cmd.Connection = Con
                    cmd.CommandText = "SELECT Distinct Vehicle_Type FROM Weightment Where Cash_Cr='Credit' AND (Weigh_date Between @dt AND @dt2) Group by Vehicle_Type"
                    cmd.Parameters.AddWithValue("@dt", DateTime.Parse(dt))
                    cmd.Parameters.AddWithValue("@dt2", DateTime.Parse(dt2))
                    Dim da As SqlDataReader = cmd.ExecuteReader
                    While da.Read
                        Credit_report_daily.DataGridView1.Columns.Add(da.Item(0), da.Item(0))
                    End While
                    da.Close()
                    Con.Close()

2 个答案:

答案 0 :(得分:0)

添加Party_name的计数,即select Party_name,count(Party_name) FROM Weightment...。第二个查询也一样。

答案 1 :(得分:0)

我不知道CTE是否有用或适用于您的情况。但其中一个解决办法就是这个。

With CTEPartyCount as 
(SELECT Party_name, ROW_NUMBER() over (partition by Truck order by Truck) AS Truck_Count,
ROW_NUMBER() over (partition by Turbo order by Turbo) AS Turbo_Count
FROM Weightment Where Cash_Cr='Credit' AND (Weigh_date Between @dt AND @dt2)
)

Select distinct Party_name,max(Truck_Count),max(Turbo_Count) from CTEPartyCount
group by Party_name