日期:2014-05-18  浏览次数:20484 次

求一复杂的sql语句
sql   server   2000
表结构   starttime   datetime
              movie   varchar
              id   int

表中数据举例:
            id     starttime                         movie
            1       2012-03-22   08:00:00       aa
            1       2012-03-22   13:20:00       ab
            1       2012-03-23   10:50:00       ac
            1       2012-03-23   16:00:00       ad
            2       。。。
            2       。。。
            3       。。。
现在给定个起始时间和结束时间(起始时间和结束时间可以相差一天也可以相差多天),查询在这个时间段中,各个id所产生的数据的条数,关键:每天的13:00到下一天的13:00之间,同一个id无论多少条数据,都算一条。
期望效果:起始时间:2012-03-22   13:00:00     结束时间:2012-03-23   13:00:00
结果为:
              id           数量
                1             1
                2           。。。

------解决方案--------------------
少写了个别名.不好意思.

select id , count(1) cnt from

select distinct id , convert(varchar(10),dateadd(hh , -13 , starttime) ,120) starttime from tb where starttime between '2012-03-22 13:00:00' and '2012-03-23 13:00:00'
) t
group by id

--count(1)不是就把数量写死了是1,是在统计每个ID,和count(*)一个意思.