现在系统中有这样一个功能。  系统会每隔3分钟获取系统的一些数据并存入数据据,数据库格式很简单: id     time(dateTime类型)     data    就这么三列.
现在给定一个月,要获取这个月内每天的最后一条数据。各位有没有比较好的SQL解决办法? 初始版本中是查出这个月的所有数据,然后在程序中处理的。 效率不怎么高, 想问下各位能不能在SQL上做点文章?  如果不行的话,只能去优化程序了。~
   

解决方案 »

  1.   

    select * from @t as A
    where datediff(month, '2011-11-1',[time])=0
    and 
    not exists(select 1 from @t where datediff(day,[time],A.[time])=0 and [time]>A.[time])
      

  2.   


    用了很多函数~     悲剧的是我用的是MongoDB存储数据,这些函数估计不存在
      

  3.   


    ;with cte_play
    as
    (
    select tempcol= row_number() over (partition by convert(char(10),[time],120) order by [time] desc) , * from @t 
    where datediff(month,'2011-1-1',[time])=0
    )
    select id,[time],data from cte_play
    where tempcol=1
      

  4.   

    select * from TB T where not exists(select 1 from TB where convert(varchar(7),time,120)= convert(varchar(7),t.time,120)
    and time >t.time) ???
      

  5.   

    杯具了,
    MongoDB 的不懂
      

  6.   

    杯具了,
    MongoDB 的不懂 
      

  7.   


    Mongo DB ??? 不懂...
      

  8.   

    select
      distinct b.*
    from
      tb a
    cross apply
      (select top 1 * from tb where convert(varchar(10),[time],120)=convert(varchar(10),t.[time],120) order by time desc)b
      

  9.   

    MongoDB数据库,唉!对会长同情,看那语法就纠结,贴一段:查询colls所有数据db.colls.find() //select * from colls 通过指定条件查询db.colls.find({‘last_name’: ‘Smith’});//select * from colls where last_name=’Smith’ 指定多条件查询db.colls.find( { x : 3, y : “foo” } );//select * from colls where x=3 and y=’foo’ 指定条件范围查询db.colls.find({j: {$ne: 3}, k: {$gt: 10} });//select * from colls where j!=3 and k>10 查询不包括某内容db.colls.find({}, {a:0});//查询除a为0外的所有数据 支持<, <=, >, >=查询,需用符号替代分别为$lt,$lte,$gt,$gtedb.colls.find({ “field” : { $gt: value } } );  db.colls.find({ “field” : { $lt: value } } );  db.colls.find({ “field” : { $gte: value } } ); db.colls.find({ “field” : { $lte: value } } ); 也可对某一字段做范围查询db.colls.find({ “field” : { $gt: value1, $lt: value2 } } ); 不等于查询用字符$nedb.colls.find( { x : { $ne : 3 } } ); in查询用字符$indb.colls.find( { “field” : { $in : array } } );db.colls.find({j:{$in: [2,4,6]}}); not in查询用字符$nindb.colls.find({j:{$nin: [2,4,6]}}); 取模查询用字符$moddb.colls.find( { a : { $mod : [ 10 , 1 ] } } )// where a % 10 == 1还没有多表查询的,没有日期处理函数的,只能程序里循环取了,优化。
      

  10.   

    MongoDB建議去其它數據庫問問看