已经表的信息如下  
日期    状态                      
1       未到诊    
1       初诊      
2       复诊      
5       初诊      
2       复诊      
..
...
....
我想在界面的datagridivew中显示如下所示的信息日期   记录总数  到诊数量(初诊+复诊)   到诊率(初诊数量/记录总数)         
1        10            2                       20%
2        10            4                       40%
3        20            5                       25%
..
...
....注:若是单独球记录总数 和 到诊数量 我是会的 ,如下->
1.记录总数: select count(ID) from 表  group by 日期 order by 日期 desc
2.到诊总数: select count(ID) from 表 where 状态=“初诊” or 状态="复诊"  group by 日期  order by 日期 desc
但是两个sql整合到一起(别忘了还有个到诊率),最后出现到上面datagridview那样,我就不太会弄了。
PS:如果到诊率不用sql直接取出,请写出C#代码或给个思路,谢谢了!!!!!!

解决方案 »

  1.   

    selet 1,2 from (
    记录总数: select count(ID)as 1 from 表 group by 日期 order by 日期 desc
    union
    到诊总数: select count(ID)as 2 from 表 where 状态=“初诊” or 状态="复诊" group by 日期 order by 日期 desc
    )
      

  2.   

    这个简单  不需要SQL 直接计算 for (int i = 0; i < dataGridView1.RowCount; i++)
                {
                    dataGridView1[3, i].Value = (double)dataGridView1[1, i].Value / (double)dataGridView1[2, i].Value+"%";
                }
      

  3.   

    select table.*,A.AllCount,B.Count1,B.count1/A.AllCount from table left join 
    (select count(ID) as AllCount,id from table  group by 日期 order by 日期 desc) A
    on table.id=A.id left join 
    (select count(ID)as count1,id from table where 状态=“初诊” or 状态="复诊" group by 日期 order by 日期 desc)B on Table.id=B.id
      

  4.   

    思路是这样子  
    不过我没调试  还有订正下
         for (int i = 0; i < dataGridView1.RowCount; i++)
                {
                    dataGridView1[3, i].Value = (double)dataGridView1[1, i].Value*100 / (double)dataGridView1[2, i].Value+"%";
                }
      

  5.   

    你先按 group 状态进行分组,将结果集插入 临时表里,然后再取出来附加上其他的列!
      

  6.   

    create table test values (
    workday int,
    worktype nvarchar(10))insert into test values(1,'初诊')
    insert into test values(1,'初诊')
    insert into test values(1,'未到诊')
    insert into test  values(1,'初诊')
    insert into test  values(1,'复诊')
    insert into test  values(1,'未到诊')
    insert into test values(2,'初诊')
    insert into test values(2,'未到诊')insert into test values(3,'初诊')
    insert into test values(3,'未到诊')
    insert into test values(3,'复诊')
    insert into test values(3,'复诊')
    insert into test values(3,'未到诊')select * from test order by workday
    select workday,count(1) as 总数,sum(case when worktype='初诊' then 1 else 0 end + case when worktype='复诊' then 1 else 0 end)  AS 到诊数量,sum(cast(case when worktype='初诊' then 1 else 0 end + case when worktype='复诊' then 1 else 0 end   as decimal))/count(1)
    from test group by workday
      

  7.   

    更正一下,初诊率select workday,count(1) as 总数,sum(case when worktype='初诊' then 1 else 0 end + case when worktype='复诊' then 1 else 0 end)  AS 到诊数量,sum(cast(case when worktype='初诊' then 1 else 0 end    as decimal))/count(1)
    from test group by workday
      

  8.   

    select 
      日期,
      count(id) as 记录总数,
      sum(case 状态='初诊' or 状态='复诊' then 1 else  0 end) as 到诊总数,
      ltrim(cast(sum(case 状态='初诊' or 状态='复诊' then 1 else  0 end)*100.0/count(id) as dec(18,2)))+'%' as 到诊率
    from
      tb
    group by
      日期你分挺多的!