各位大侠,请教一个SQL语句,比如说我的一张表里每个时刻在医院看病的人的情况。里面有至少有两个字段,发病时间(发病时间精确到一天的每分钟)和疾病名称。现在我想统计某种疾病在每天的发病数量。比如(2006-01-01到2006-02-01期间每天水痘患者的数量。)这个SQL怎么写呢?

解决方案 »

  1.   

    SELECT COUNT(username) FROM PatientTable WHERE StartTime>=CONVERT(Datetime,'2006-01-01') AND StartTime<=CONVERT(Datetime,'2006-02-01')
      

  2.   

    select count(id) from table where 疾病名称='水痘' and 发病数量 between '2006-01-01' and '2006-02-01'
      

  3.   

    select   count(id)   from   table   where   疾病名称='水痘'   and   发病时间   between   '2006-01-01'   and   '2006-02-01'
      

  4.   

    select    发病时间,    count(*)       from       table       where       疾病名称='水痘'       and       发病时间       between       '2006-01-01'       and       '2006-02-01' group by  发病时间
      

  5.   

    select count(id) from table where 疾病名称='水痘' and 发病数量 between '2006-01-01' and '2006-02-01'
      

  6.   

    select DatePart( month,发病时间)+ '月' + DatePart( day, 发病时间) + '日' AS 发病日期, count(*)AS Num from   table  where   疾病名称='水痘' 
    and  发病时间  between  '2006-01-01'   and   '2006-02-01'  group   by     DatePart( month,发病时间), DatePart( day, 发病时间)
      

  7.   

    select   count(id)   from   table   where   疾病名称='水痘'   and   发病数量   between   '2006-01-01'   and   '2006-02-01'
      

  8.   

    select   count(id)   from   table   where   疾病名称='水痘'   and   发病数量   between   '2006-01-01'   and   '2006-02-01'
      

  9.   

    返回的DataTable应该有两列。一列是时间,一列是发病数量。
      

  10.   

    select trunc(发病日期) AS 时间,count(*) AS 数量
    FROM disease.INFECTIOUS
    where 发病日期 between to_date('2006-01-01','yyyy-mm-dd')
    and to_date('2007-02-01','yyyy-mm-dd')
    and 疾病编号='水痘'
    group by trunc(发病日期),疾病编号 ORDER BY 时间 ASC   这样写对于单个是对的。
    1 4
    2 2007-1-26 5
    3 2007-1-27 8
    4 2007-1-28 7
    5 2007-1-29 3
    6 2007-1-30 9
    7 2007-1-31 6
    8 2007-2-1 8
    但如果是所有的疾病的话。语句如下
    select trunc(发病日期) AS 时间,count(*) AS 数量
    FROM disease.INFECTIOUS
    where 发病日期 between to_date('2006-01-01','yyyy-mm-dd')
    and to_date('2007-02-01','yyyy-mm-dd')
    group by trunc(发病日期),疾病编号 ORDER BY 时间 ASC   这样写对于单个是对的。但结果好像不对
    1 2007-1-25 1
    2 2007-1-25 1
    3 2007-1-25 4
    4 2007-1-25 1
    5 2007-1-25 2
    6 2007-1-25 3
    7 2007-1-26 5
    8 2007-1-26 1
    9 2007-1-26 2
    10 2007-1-26 1
    11 2007-1-26 1
    12 2007-1-26 3
    13 2007-1-26 1
    14 2007-1-26 1
    15 2007-1-26 1
    16 2007-1-26 3
    17 2007-1-26 1
    18 2007-1-26 1
    19 2007-1-26 1
    20 2007-1-26 1
    21 2007-1-27 2
    这个是怎么回事呢?
      

  11.   

    废话,下面这个是所有疾病的,上面那个只是水痘的
    要所有疾病分组效果应该这样select   trunc(发病日期)   AS   时间,count(*)   AS   数量 
    FROM   disease.INFECTIOUS 
    where   发病日期   between   to_date('2006-01-01','yyyy-mm-dd') 
    and   to_date('2007-02-01','yyyy-mm-dd') 
    group   by   trunc(发病日期)   ORDER   BY   时间   ASC