Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7736279
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T07:42:09+00:00 2026-06-01T07:42:09+00:00

Using Rails ActiveRecord, am wanting to calculate the time between two datetime fields within

  • 0

Using Rails ActiveRecord, am wanting to calculate the time between two datetime fields within the same row. The difference between the two datetimes could be seconds through to hours, and could span mid-night. A number of seconds representing the difference would be dandy.

Am using both sqlite and postgress.

Playing with sqlite, the following just gives 0 for the calculation. (both “solved_at” and “started_at” are type “datetime”, and “solved_at” is later than “started_at”)

SELECT  (datetime(solved_at) - datetime(started_at)) FROM "schedules";
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T07:42:10+00:00Added an answer on June 1, 2026 at 7:42 am

    something like this:

    dur_sql = "strftime('%s',solved_at) - strftime('%s',started_at)" unless Rails.env.production? # sqlite in dev and test modes    
    dur_sql = "extract( epoch from solved_at - started_at)" if Rails.env.production? # heroku with postgres
    
    select_clause = "count(*) as count, env, case_type, date(started_at) as started_at_date, " + "AVG(#{dur_sql}) as avg_dur, MIN(#{dur_sql}) as min_dur, MAX(#{dur_sql}) as max_dur"
    
    group_clause = "env, case_type, date(started_at)"
    
    where_clause = ...
    grouped_stuff = Schedule.select( select_clause ).where( where_clause ).group( group_clause )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using rails and am trying to figure out how to use ActiveRecord within
I am using Rails 2.3.5. Class User < ActiveRecord::Base has_many :phones end class Phone
Rails uses the concept of migrations to deal with model changes using the ActiveRecord
Recently thanks to rails' popularity, many people start using activerecord as model. however, before
I'm using Rails 3 w/ Mongoid, (so no ActiveRecord). Mongoid uses ActiveModel's to_json method,
I recently added a :title column using Rails migration: class AddTitleToMicroposts < ActiveRecord::Migration def
Using Rails 2.3.14, I'm looking for way to access the owner of an ActiveRecord
I'm using rails 3, and began my application with ActiveRecord. Now, I have many
I am using Rails 3.0.8 Lets say that I have two collections of users.
Let's assume I have two Rails ActiveRecord models, CarType (make/model of car) and Car

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.