我想实现:比如:SYTB表中有个字段名为syclass,而这个字段的可能为值:111,222,333;我想统计SYTB 表中syclass分别取这三个值的个数应怎么样实现??????高手们帮帮忙!!!!!!!!

解决方案 »

  1.   

    看看是不是你想要的:
    select count(case syclass when "111"),count(case syclass when "222"),count(case syclass when "333") from SYTB
      

  2.   

    Select Count(*) From SYTB Where syclass = 111统计的是 syclass字段的值为 111 的记录总数另外两个类似,
      

  3.   

    上面的SQL语句得到的结果怎么样用在别的地方啊?因为要用这个结果来做统计图表
      

  4.   

    string num1,num2;
    string connstr="workstation id=IMAGE;packet              size=4096;Trusted_Connection=Yes;data source=localhost;persist security info=False;initial catalog=KYGLdatabase ";
    SqlConnection sqlConn1=new SqlConnection( connstr); string mySelectQuery="SELECT * FROM sytb"; SqlCommand myCommand = new SqlCommand(mySelectQuery,sqlConn1);
    sqlConn1.Open(); SqlDataAdapter Adapter = new SqlDataAdapter();
    Adapter.SelectCommand= myCommand;
    DataSet myDs = new DataSet();
    Adapter.Fill(myDs,"sy");
    SqlCommandBuilder MyCommandBuild=new SqlCommandBuilder (Adapter); foreach(DataRow rowIndex in myDs.Tables["sy"].Rows)
    {
    if(rowIndex["sy"].ToString()="111")
    num1=num1+1;
    else 
    num2=num2+1;

    }这样为什么得不到num1,num2的值呢?if(rowIndex["sy"].ToString()="111")这句错在哪
      

  5.   

    抱歉,刚才写错了
    select 
    Count(case syclass when "111" then syclass end  ) ,Count(case syclass when "222" then syclass end  ),Count(case syclass when "333" then syclass end  )
    from SYTB
      

  6.   

    把你查询所得的结果用在统计图表就行了呀,比如说,syclass 的值为111的记录总数为100,在你的统计图表中显示100,不知你是否想在你的统计表中显示查询的记录数。
      

  7.   

    if(rowIndex["sy"].ToString()="111")=====>if(rowIndex["sy"].ToString().Trim()=="111")把=换成 ==,不知道你编译怎么通过的,另外用循环来做这种事情不好,太慢,可以考虑用
    DataView dv = new DataView(myDs.Tables["sy"],"sy='111'","", DataViewRowState.CurrentRows)if (dv.Count>0)
       int counter = dv.Count另外你这三个字段是什么类型的需要搞清楚
      

  8.   

    是啊
    就是想要查询的记录数啊怎样查询所得的结果赋给统计图表参数num1,num2,num3
    ?
      

  9.   

    foreach(DataRow rowIndex in myDs.Tables["sy"].Rows)
    {
    if(rowIndex["syclass"].ToString() == "111")
    num1 += 1;
    elseif(rowIndex["syclass"].ToString() == "222")  
    num2 += 1;
                                         else
                                                  num3 += 1;

    }
      

  10.   

    tryselect distinct syclass,count(*) from SYTB
      

  11.   

    sorry
    try
    select distinct syclass,count(*) from SYTB group by syclasslike:
    SELECT DISTINCT CategoryID, COUNT(*) AS Expr1
    FROM Products
    GROUP BY CategoryIDresult:CategoryID        Expr1
    1 12
    2 12
    3 13
    4 10
    5 7
    6 6
    7 5
    8 12
      

  12.   

    string TableWidth = "80%"; //整体宽度 
    string TableHeight = "110"; //整体高度 
    string fontsize = "2"; //字体大小 
    string fontcolor = "#999999"; //字体颜色 
    string strnum = "num1,num2"; //统计对象数据 数据间以“,”分隔 
    string strmemo = "重点项目,面上项目"; //统计对象说明 说明间已“,”分隔            MainPic ShowPic = new MainPic(); 
    ShowPic.GetImageHTML(TableWidth,TableHeight,fontsize,fontcolor,strnum,strmemo); mainpic是已经封装的一个通用类这样赋值是错的,请问错在哪,接我前面的代码的
      

  13.   

    string strnum = "num1,num2"; 当这里直接给出string strnum = "23,30"; 时可以正常运行
      

  14.   

    string strnum = num1.ToString()+","+num2.ToString();
      

  15.   

    string strnum = num1.ToString()+","+num2.ToString();
    得到的是一些二进制数吗?因为我将其显示出来是,全是111,111111而且划成10进制结果好象也不对啊
      

  16.   

    你的程序中num1,num2是怎么得到的,一点代码提示都没有,我们怎么知道你得的是什么值呢?
      

  17.   

    string num1,num2;
    string connstr="workstation id=IMAGE;packet              size=4096;Trusted_Connection=Yes;data source=localhost;persist security info=False;initial catalog=KYGLdatabase ";
    SqlConnection sqlConn1=new SqlConnection( connstr); string mySelectQuery="SELECT * FROM sytb"; SqlCommand myCommand = new SqlCommand(mySelectQuery,sqlConn1);
    sqlConn1.Open(); SqlDataAdapter Adapter = new SqlDataAdapter();
    Adapter.SelectCommand= myCommand;
    DataSet myDs = new DataSet();
    Adapter.Fill(myDs,"sy");
    SqlCommandBuilder MyCommandBuild=new SqlCommandBuilder (Adapter); foreach(DataRow rowIndex in myDs.Tables["sy"].Rows)
    {
    if(rowIndex["sy"].ToString()="111")
    num1=num1+1;
    else 
    num2=num2+1;

    }
      

  18.   

    string strnum = Convert.ToString(num1)+","+Convert.ToString(num2);
      

  19.   

    SqlConnection sqlConn1=new SqlConnection("data source=localhost;persist security info=False;initial catalog=KYGLdatabase ");SqlCommand myCommand = new SqlCommand("select distinct syclass,count(*) As ComputerColumn from SYTB group by syclass",sqlConn1);sqlConn1.Open();
    SqlDataReader dr = myCommand.ExecuteReader();
    string strnum=null;
    do
    {
       strnum+=Convert.ToString(dr.GetInt32(1))+","} while (myReader.NextResult());strnum = strnum.SubString(0,strnum.Length -1);
    dr.Close();
    sqlConn1.Close();
      

  20.   

    SqlConnection sqlConn1=new SqlConnection("data source=localhost;persist security info=False;initial catalog=KYGLdatabase ");SqlCommand myCommand = new SqlCommand("select distinct syclass,count(*) As ComputerColumn from SYTB group by syclass",sqlConn1);sqlConn1.Open();
    SqlDataReader dr = myCommand.ExecuteReader();
    string strnum=null;
    do
    {
       strnum+=Convert.ToString(dr.GetInt32(1))+","} while (dr.NextResult());strnum = strnum.SubString(0,strnum.Length -1);
    dr.Close();
    sqlConn1.Close();
      

  21.   

    strnum = strnum.SubString(0,strnum.Length -1);这句编译不过提示: string并不包含对SubString的定义
      

  22.   

    using System;
    using System.Text;真的比较菜啊!开玩笑了
      

  23.   

    strnum = strnum.SubString(0,strnum.Length -1);
    ===》
    strnum = strnum.Substring(0,strnum.Length -1);
      

  24.   

    我已经解决了string num1,num2;定义成int num1,num2;就好了谢谢无间道