- 论坛徽章:
- 0
|
按时间段分组
PostgreSQL:
Ruby代码- 1.User.count(:order => 'DATE(created_at) DESC', :group => ["DATE(created_at)"])
- User.count(:order => 'DATE(created_at) DESC', :group => ["DATE(created_at)"])
复制代码 Ruby代码- 1.User.count(:order => 'DATE(created_at) DESC', :group => ["DATE(created_at)"]).each {|u| puts "#{u[0]} -> #{u[1]}" }
- User.count(:order => 'DATE(created_at) DESC', :group => ["DATE(created_at)"]).each {|u| puts "#{u[0]} -> #{u[1]}" }
复制代码 Ruby代码- 1.Foo.order("DATE(start_at)").group("DATE(start_at)").count
- Foo.order("DATE(start_at)").group("DATE(start_at)").count
复制代码 Mysql在另一篇,heroku啊,让我学习postgresql
Ruby代码- 1.select floor(datediff(now(), created) / 3) * 3 as days_ago
- 2. ,min(created)
- 3. ,max(created)
- 4. ,count(*)
- 5. from t1
- 6. group
- 7. by floor(datediff(now(), created) / 3);
- select floor(datediff(now(), created) / 3) * 3 as days_ago
- ,min(created)
- ,max(created)
- ,count(*)
- from t1
- group
- by floor(datediff(now(), created) / 3);
复制代码 https://github.com/Bantik/seer
Ruby代码- 1.# declare a struct to hold the results
- 2.UserCountByDate = Struct.new(:date, :count)
- 3.
- 4.def report
- 5. @user_counts = User.count( :group => "DATE(created_at)",
- 6. :conditions => ["created_at >= ? ", 7.days.ago],
- 7. :order => "DATE(created_at) ASC"
- 8. ).collect do |date, count|
- 9. UserCountByDate.new(date, count)
- 10. end
- 11.
- 12.end
- # declare a struct to hold the results
- UserCountByDate = Struct.new(:date, :count)
- def report
- @user_counts = User.count( :group => "DATE(created_at)",
- :conditions => ["created_at >= ? ", 7.days.ago],
- :order => "DATE(created_at) ASC"
- ).collect do |date, count|
- UserCountByDate.new(date, count)
- end
复制代码 end
Html代码- 1.<div id="chart"></div>
- 2.
- 3.<%= Seer::visualize(
- 4. @user_counts,
- 5. :as => :column_chart,
- 6. :in_element =>'chart',
- 7. :series => {
- 8. :series_label => 'date',
- 9. :data_method => 'count'
- 10. },
- 11. :chart_options => {
- 12. :height => 300,
- 13. :width => 100 * @user_counts.size,
- 14. :is_3_d => true,
- 15. :legend => 'none',
- 16. :colors => "[{color:'#990000', darker:'#660000'}]",
- 17. :title => "New users in last 7 days",
- 18. :title_x => 'date',
- 19. :title_y => 'count'
- 20. }
- 21. )
- 22. -%>
复制代码 |
|