有一张表"sample" 结构如下
ID(Int)  Time(DateTime)2个字段要求根据时间间隔 不如
1. 某一天(指定的)中 X个小时 为间隔 的记录条数 (从0:00开始)
2. 某一月(指定的)中 X个天 为间隔 的记录条数(从1号开始)
3. 某一年(指定的)中 X个月为间隔 的记录条数(从一月开始)数据库是SQL Server,可以用Transact_SQL
谢谢

解决方案 »

  1.   

    1.select * from table
    where DATEPART (Hour,日期)<10 and DATEPART (day,日期)=10
    and DATEPART(month,日期)=11 and DATEPART(year,日期)=2003
    2.select * from table
    where DATEPART (day,日期)<10 and DATEPART(month,日期)=11 and DATEPART(year,日期)=2003
    3. select * from table
    where DATEPART (month,日期)<11 and DATEPART(year,日期)=2003
      

  2.   

    忘记了
    改select * from table

    select count(*) from table
      

  3.   

    select count(*) from table
    where 日期>getdate() and 日期>DATEADD(Hour,2, getdate())
      

  4.   

    TO:楼主
    那个字念“jue(珏)”!
      

  5.   

    1:
    select count(*) from sample
    where 日期=指定的日期
    group by trunc(DATEPART (Hour,日期)/X) 
    2:
    select count(*) from sample 
    where month(日期)=指定月 and year(日期)=指定年
    group by trunc(DATEPART (day,日期)/X)
    3:
    select count(*) from sample
    where year(日期)=指定年
    group by trunc(DATEPART (month,日期)/X)
      

  6.   

    你说的意思有些晦涩,我是这样理解的,不知道对不对:1. 某一天(指定的)中 X个小时 为间隔 的记录条数 (从0:00开始)
      间隔X个小时是指从0:00开始再加上X个小时,比如间隔2小时就是指从01:00:00至03:00:002. 某一月(指定的)中 X个天 为间隔 的记录条数(从1号开始)
      同样,间隔 5 天就是指从1号算起至5号3. 某一年(指定的)中 X个月为间隔 的记录条数(从一月开始)
     以此类推因此,SQL如下:select sum(SumHour) SumHour,sum(SumDay) SumDay,Sum(SumMonth) SumMonth 
    from (
          select case  when (convert(char(10), times, 20)='2003-01-01') and
                      (times<=dateadd(hour,2,convert(char(10), times, 20))) then 1 else 0 end SumHour,
                 case  when (datepart(mm,times)=4 and datepart(dd,times)<=3) then 1 else 0 end SumDay,
                 case  when (datepart(yy,times)=2003 and datepart(mm,times)<=4) then 1 else 0 end SumMonth 
            from A1) 
    TempA1其中:
        1.'2003-01-01'是指定天数,dateadd(hour,2,convert(char(10), times, 20)指间隔时间段(2小时);
        2.datepart(mm,times)=4 中的4是指定月份,datepart(dd,times)<=3指间隔天数(3天);
      3.datepart(yy,times)=2003 中的2003指定年份,datepart(mm,times)<=4指间隔月份(4个月);
      

  7.   

    其中表结构和测试数据:create table A1(
      Seq int not null,
      Times DateTime
      Primary key(Seq)
    )insert into A1 values(1,'2003-01-01 00:30:28')
    insert into A1 values(2,'2003-01-01 01:38:28')
    insert into A1 values(3,'2003-01-01 01:58:28')
    insert into A1 values(4,'2003-04-02 13:30:28')
    insert into A1 values(5,'2003-04-03 13:20:28')
      

  8.   

    select count(*) from table
    where 日期>getdate() and 日期>DATEADD(Hour,2, getdate())
      

  9.   

    to (月满西星) 好像 你的写法是我需要的结果 挺复杂,仔细看看再说我的要得结果就像如下
    1.如果一天2小时间隔的话 就是12间隔, 结果:字段: s0_2 s2_4 s4_6 ...s22_24
    值:   1    6    0       8这样子,0:00至2:00母表记录数是1条;
            2:00至4:00母表记录数是6条;
            4:00至6:00母表记录数是0条;
            ...
    当然天是指定的。 每隔的数据以记录形式也行(12条记录),这里是用字段的啦(12个字段) 
      

  10.   

    To noil0125(珏心)Sorry! 
    你的名字不识的说,同时又感觉"玉心"蛮有灵气 哈哈哈
      

  11.   

    to (月满西星) 好像 你的写法是我需要的结果 挺复杂,仔细看看再说我的要得结果就像如下
    1.如果一天2小时间隔的话 就是12间隔, 结果:字段: s0_2 s2_4 s4_6 ...s22_24
    值:   1    6    0       8这样子,0:00至2:00母表记录数是1条;
            2:00至4:00母表记录数是6条;
            4:00至6:00母表记录数是0条;
            ...
    当然天是指定的。 每隔的数据以记录形式也行(12条记录),这里是用字段的啦(12个字段) 
    =============================================================================针对上面,就以时间间隔为例:以记录形式显示:
    select count(Times) HourCount
    from (select (case 
                    when datepart(hour,times) between 0 and 2 then 1 
                    when datepart(hour,times) between 3 and 4 then 2 
                    when datepart(hour,times) between 5 and 6 then 3 
                    when datepart(hour,times) between 7 and 8 then 4 
                    when datepart(hour,times) between 9 and 10 then 5 
                    when datepart(hour,times) between 11 and 12 then 6 
                    when datepart(hour,times) between 13 and 14 then 7 
                    when datepart(hour,times) between 15 and 16 then 8 
                    when datepart(hour,times) between 17 and 18 then 9
                    when datepart(hour,times) between 19 and 20 then 10
                    when datepart(hour,times) between 21 and 22 then 11
                    else 12
                   end) Times
            from A1) A11
    group by Times
    以字段形式显示:
    select sum(case when datepart(hour,times) between 0 and 2 then 1 else 0 end) 第一时段,
           sum(case when datepart(hour,times) between 3 and 4 then 1 else 0 end) 第二时段,
           sum(case when datepart(hour,times) between 5 and 6 then 1 else 0 end) 第三时段,
           sum(case when datepart(hour,times) between 7 and 8 then 1 else 0 end) 第四时段,
           sum(case when datepart(hour,times) between 9 and 10 then 1 else 0 end) 第五时段,
           sum(case when datepart(hour,times) between 11 and 12 then 1 else 0 end) 第六时段,
           sum(case when datepart(hour,times) between 13 and 14 then 1 else 0 end) 第七时段,
           sum(case when datepart(hour,times) between 15 and 16 then 1 else 0 end) 第八时段,
           sum(case when datepart(hour,times) between 17 and 18 then 1 else 0 end) 第九时段,
           sum(case when datepart(hour,times) between 19 and 20 then 1 else 0 end) 第十时段,
           sum(case when datepart(hour,times) between 21 and 22 then 1 else 0 end) 第十一时段,
           sum(case when datepart(hour,times) between 23 and 24 then 1 else 0 end) 第十二时段
    from A1其中:1.我没有指定天数,该条件可在when语句中加入;
         2.间隔时段以2小时为间隔;
         3.以记录形式显示只会显示数据表中存在的记录,比如:
           7-8这个时段没有记录则不会显示;
         4.其它显示天数和月份的原理相同,故没有列出。
         5.不知道还有没有更好的写法,感觉SQL太长了,读起来不爽-_-
      

  12.   

    查找1天: between Sysdate-1 and Sysdate
    查找N天: between Sysdate-N and Sysdate查找1小时: between Sysdate-1/24 and Sysdate
    查找H小时: between Sysdate-H/24 and Sysdate查找1分钟: between Sysdate-1/1440 and Sysdate
    查找M分钟: between Sysdate-M/1440 and Sysdate
      

  13.   

    不是用Transact_SQL吗?declare Variable int
    set Variable=2就可以了。