免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3308 | 回复: 2
打印 上一主题 下一主题

按时间段分组 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-12-03 11:15 |只看该作者 |倒序浏览
按时间段分组








PostgreSQL:

Ruby代码
  1. 1.User.count(:order => 'DATE(created_at) DESC', :group => ["DATE(created_at)"])  
  2. User.count(:order => 'DATE(created_at) DESC', :group => ["DATE(created_at)"])
复制代码
Ruby代码
  1. 1.User.count(:order => 'DATE(created_at) DESC', :group => ["DATE(created_at)"]).each {|u| puts "#{u[0]} -> #{u[1]}" }  
  2. User.count(:order => 'DATE(created_at) DESC', :group => ["DATE(created_at)"]).each {|u| puts "#{u[0]} -> #{u[1]}" }
复制代码
Ruby代码
  1. 1.Foo.order("DATE(start_at)").group("DATE(start_at)").count  
  2. Foo.order("DATE(start_at)").group("DATE(start_at)").count
复制代码
Mysql在另一篇,heroku啊,让我学习postgresql


Ruby代码
  1. 1.select floor(datediff(now(), created) / 3) * 3 as days_ago   
  2. 2.      ,min(created)   
  3. 3.      ,max(created)   
  4. 4.      ,count(*)   
  5. 5.  from t1   
  6. 6. group   
  7. 7.    by floor(datediff(now(), created) / 3);  
  8. select floor(datediff(now(), created) / 3) * 3 as days_ago
  9.       ,min(created)
  10.       ,max(created)
  11.       ,count(*)
  12.   from t1
  13. group
  14.     by floor(datediff(now(), created) / 3);
复制代码
https://github.com/Bantik/seer


Ruby代码
  1. 1.# declare a struct to hold the results   
  2. 2.UserCountByDate = Struct.new(:date, :count)   
  3. 3.  
  4. 4.def report   
  5. 5.  @user_counts = User.count( :group => "DATE(created_at)",   
  6. 6.                   :conditions => ["created_at >= ? ", 7.days.ago],   
  7. 7.                   :order => "DATE(created_at) ASC"  
  8. 8.                 ).collect do |date, count|   
  9. 9.                   UserCountByDate.new(date, count)   
  10. 10.                 end  
  11. 11.  
  12. 12.end  
  13. # declare a struct to hold the results
  14. UserCountByDate = Struct.new(:date, :count)

  15. def report
  16.   @user_counts = User.count( :group => "DATE(created_at)",
  17.                    :conditions => ["created_at >= ? ", 7.days.ago],
  18.                    :order => "DATE(created_at) ASC"
  19.                  ).collect do |date, count|
  20.                    UserCountByDate.new(date, count)
  21.                  end
复制代码
end




Html代码
  1. 1.<div id="chart"></div>  
  2. 2.  
  3. 3.<%= Seer::visualize(   
  4. 4.      @user_counts,   
  5. 5.      :as => :column_chart,   
  6. 6.      :in_element =>'chart',   
  7. 7.      :series => {   
  8. 8.        :series_label => 'date',   
  9. 9.        :data_method => 'count'   
  10. 10.      },   
  11. 11.      :chart_options => {   
  12. 12.        :height => 300,   
  13. 13.        :width => 100 * @user_counts.size,   
  14. 14.        :is_3_d => true,   
  15. 15.        :legend => 'none',   
  16. 16.        :colors => "[{color:'#990000', darker:'#660000'}]",   
  17. 17.        :title => "New users in last 7 days",   
  18. 18.        :title_x => 'date',   
  19. 19.        :title_y => 'count'   
  20. 20.      }   
  21. 21.    )   
  22. 22. -%>  
复制代码

论坛徽章:
0
2 [报告]
发表于 2011-12-21 22:17 |只看该作者
谢谢分享  希望于楼主多多交流

论坛徽章:
0
3 [报告]
发表于 2011-12-22 10:04 |只看该作者
PostgreSQL其实挺好的,为啥PostgreSQL用户这么少呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP