代码之家  ›  专栏  ›  技术社区  ›  Prateek Jahagirdar

从ASP.NET中的SQL Server获取汇总/排序的数据

  •  1
  • Prateek Jahagirdar  · 技术社区  · 6 年前

    下面是我保存学生出勤率的SQL表

    我正在尝试创建一个汇总的出勤报告,如下所示:

    所有学生的出勤总结:

    • 一班学生人数:1人
    • 二班学生人数:1人
    • 三班学生人数:0人

    但我并没有找到正确的方法。下面是我试图用来实现这一点的示例代码:

    public void getPresentCount())
    {
    使用(sqlconnection con=new sqlconnection(constring))
    {
    con.open();
    使用(sqlcommand cmd=new sqlcommand(“select count(*)as'totalpresent'from stud揤att where att='present'and a揤date='+systemdate+“'”,con))。
    {
    使用(sqldatareader dr=cmd.executereader())
    {
    if(dr.read())
    {
    totstrends.innerText=dr[“totalCount”].toString();
    }
    其他的
    {
    tottress.innertext=“0”;
    }
    }
    }
    con.close();
    }
    }
    public void getabsentCount()。
    {
    使用(sqlconnection con=new sqlconnection(constring))
    {
    con.open();
    使用(sqlcommand cmd=new sqlcommand(“select count(*)as'totalpresent'from stud揤att where att='present'and a揤date='+systemdate+“'”,con))。
    {
    使用(sqldatareader dr=cmd.executereader())
    {
    if(dr.read())
    {
    totstrends.innerText=dr[“totalCount”].toString();
    }
    其他的
    {
    tottress.innertext=“0”;
    }
    }
    }
    con.close();
    }
    }
    

    但我知道这不是正确的方法。

    我正在尝试创建一个汇总的出勤报告,如下所示:

    所有学生的出勤总结:

    • 一班学生人数:1人
    • 二班学生人数:1人
    • 三班学生人数:0人

    但我并没有找到正确的方法。下面是我试图用来实现这一点的示例代码:

    public void GetPresentCount()
    {
        using (SqlConnection con = new SqlConnection(constring))
        {
            con.Open();
            using (SqlCommand cmd = new SqlCommand("select count(*) as 'TotalPresent' from stud_att where att='Present' and a_date='"+ systemdate +"'", con))
            {
                using (SqlDataReader dr = cmd.ExecuteReader())
                {
                    if (dr.Read())
                    {
                        totstuds.InnerText = dr["TotalCount"].ToString();
                    }
                    else
                    {
                        totstuds.InnerText = "0";
                    }
                }
            }
            con.Close();
        }
    }
    public void GetAbsentCount()
    {
        using (SqlConnection con = new SqlConnection(constring))
        {
            con.Open();
            using (SqlCommand cmd = new SqlCommand("select count(*) as 'TotalPresent' from stud_att where att='Present' and a_date='" + systemdate + "'", con))
            {
                using (SqlDataReader dr = cmd.ExecuteReader())
                {
                    if (dr.Read())
                    {
                        totstuds.InnerText = dr["TotalCount"].ToString();
                    }
                    else
                    {
                        totstuds.InnerText = "0";
                    }
                }
            }
            con.Close();
        }
    }
    

    但我知道这不是正确的方法。

    1 回复  |  直到 6 年前
        1
  •  0
  •   Gordon Linoff    6 年前

    select class,
           sum(case when att = 'Present' then 1 else 0 end) as attendance
    from stud_att
    where a_date = ?
    group by class
    order by class;
    

    ?