SQL Server分组列值

时间:2016-11-21 03:48:34

标签: asp.net sql-server vb.net

我遇到了将表格列分组的问题

Private Sub BindRepeater()
    Dim cn As String = ConfigurationManager.ConnectionStrings("db").ConnectionString
    Using cs As New SqlConnection(cn)
        Using cmd As New SqlCommand("SELECT * from tbl_in_record GROUP BY in_pc_no", cs)
            Using sda As New SqlDataAdapter(cmd)
                Dim dt As New DataTable()
                sda.Fill(dt)
                rptTableIn.DataSource = dt
                rptTableIn.DataBind()
            End Using
        End Using
    End Using
End Sub

显示此错误

  

专栏' tbl_in_record.in_no'在选择列表中无效,因为它不包含在聚合函数或GROUP BY子句中。

我的表格看起来像这样

  in_id      in_pc_no      
  1   abc-def-ghi1-01  
  2   abc-def-ghi1-01  
  3   abc-def-ghi1-01  
 12   abc-def-ghi1-01  
 13   abc-def-ghi1-01  
 14   abc-def-ghi1-01  
 15   abc-def-ghi1-01  

1 个答案:

答案 0 :(得分:0)

所以,你可以使用:

SELECT DISTINCT in_pc_no FROM tbl_in_record;

SELECT in_pc_no FROM tbl_in_record GROUP BY in_pc_no;

包含的任何其他字段都需要具有一些聚合功能,例如

SELECT in_pc_no, n = Count(in_id) FROM tbl_in_record GROUP BY in_pc_no;