要求查询到
00:00 - 01:00   5条
01:00 - 02:00   10条.............     。
22:00 - 23:00    8条
23.:: - 00:00    3条要查询到一个月的按时间段的这样的SQL怎么写

解决方案 »

  1.   


    select * from table where (时间 between 开始时间 and 结束时间) and (datediff(m,时间,月份) = 0)
    //例:
    select * from table where (UploadDate between '2008-1-1 00:00' and '2008-1-1 01:00') and (datediff(m,UploadDate,'2008-1-1 00:00'))
    //当然开始时间,结束时间的月份和查询的月份要相同
      

  2.   


    create table testTable
    (
    pid int identity (1,1),
    date_created datetime,
    rowid nvarchar(64)
    )insert testTable values (dateadd(hh, 1, getdate()),  cast(newid() as varchar(64)))
    insert testTable values (dateadd(hh, 1, getdate()),  cast(newid() as varchar(64)))
    insert testTable values (dateadd(hh, 3, getdate()),  cast(newid() as varchar(64)))
    insert testTable values (dateadd(hh, 3, getdate()),  cast(newid() as varchar(64)))
    insert testTable values (dateadd(hh, 1, getdate()),  cast(newid() as varchar(64)))
    insert testTable values (dateadd(hh, 5, getdate()),  cast(newid() as varchar(64)))
    insert testTable values (dateadd(hh, 5, getdate()),  cast(newid() as varchar(64)))
    insert testTable values (dateadd(hh, 1, getdate()),  cast(newid() as varchar(64)))
    insert testTable values (dateadd(hh, 4, getdate()),  cast(newid() as varchar(64)))
    select * from testTableselect 
    datepart(MM, date_created) AS [Month], 
    datepart(dd, date_created) AS [Day], 
    datepart(hh, date_created) AS [Hour], 
    count(pid) as ItemCount
    from testtable
    group by datepart(MM, date_created), datepart(dd, date_created), datepart(hh, date_created)drop table testTable
    在Query中用月份做过滤即可
      

  3.   

    Month       Day         Hour        ItemCount
    ----------- ----------- ----------- -----------
    5           11          0           4
    5           11          2           2
    5           11          3           1
    5           11          4           2(4 row(s) affected)这是查询结果
      

  4.   

    http://www.lokcore.com/avrilxu/article.asp?id=23
    sql语句集锦
      

  5.   

    SQL语句集锦 下载 http://www.lokcore.com/download/sql精华.txt