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

解决方案 »

  1.   

    这个简单select trunc(发病时间),疾病名称,count(*)
    from test
    where 发病时间 between to_date('2006-01-01','yyyy-mm-dd')
    and to_date('2006-02-01','yyyy-mm-dd')
    and 疾病名称=水痘
    group by trunc(发病时间),疾病名称如果想统计每种病的,就把WHERE中的and 疾病名称=水痘去掉
      

  2.   

    select trunc(发病日期),疾病编号,count(*)
    FROM disease.INFECTIOUS
    where 发病日期 between to_date('2006-01-01','yyyy-mm-dd')
    and to_date('2006-02-01','yyyy-mm-dd')
    and 疾病编号='水痘'
    group by trunc(发病日期),疾病编号我改成实际的SQL 运行出来没有结果。
      

  3.   

    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
    这个是怎么回事呢?
      

  4.   

    你GROUPY里
    group   by   trunc(发病日期),疾病编号
    显示却没有"疾病编号"