calculate time difference in rails postgresql query
up vote
0
down vote
favorite
I need to get time difference as query result in my rails(which has Postgres database)
Brute Force Approach
I queried all items from my database and then iterating each record and then calculating time difference in hours which is very slow.
1) @image_retouch_items = ImageRetouchItem.where(:status => '0') = Retrieved all data
2) @image_retouch_items.each do |retouch_item|
latency_date = ((Time.parse(DateTime.now.to_s) - Time.parse(retouch_item.created_at.to_s))/3600).round
end
Optimized
I need to calculate the difference of time(hours) in query itself, how to achieve that
like - ImageRetouchItem.where(:status => '0').select('(Time.parse(DateTime.now.to_s) - Time.parse(retouch_item.created_at.to_s))/3600).round')
ruby-on-rails postgresql ruby-on-rails-3 activerecord
add a comment |
up vote
0
down vote
favorite
I need to get time difference as query result in my rails(which has Postgres database)
Brute Force Approach
I queried all items from my database and then iterating each record and then calculating time difference in hours which is very slow.
1) @image_retouch_items = ImageRetouchItem.where(:status => '0') = Retrieved all data
2) @image_retouch_items.each do |retouch_item|
latency_date = ((Time.parse(DateTime.now.to_s) - Time.parse(retouch_item.created_at.to_s))/3600).round
end
Optimized
I need to calculate the difference of time(hours) in query itself, how to achieve that
like - ImageRetouchItem.where(:status => '0').select('(Time.parse(DateTime.now.to_s) - Time.parse(retouch_item.created_at.to_s))/3600).round')
ruby-on-rails postgresql ruby-on-rails-3 activerecord
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I need to get time difference as query result in my rails(which has Postgres database)
Brute Force Approach
I queried all items from my database and then iterating each record and then calculating time difference in hours which is very slow.
1) @image_retouch_items = ImageRetouchItem.where(:status => '0') = Retrieved all data
2) @image_retouch_items.each do |retouch_item|
latency_date = ((Time.parse(DateTime.now.to_s) - Time.parse(retouch_item.created_at.to_s))/3600).round
end
Optimized
I need to calculate the difference of time(hours) in query itself, how to achieve that
like - ImageRetouchItem.where(:status => '0').select('(Time.parse(DateTime.now.to_s) - Time.parse(retouch_item.created_at.to_s))/3600).round')
ruby-on-rails postgresql ruby-on-rails-3 activerecord
I need to get time difference as query result in my rails(which has Postgres database)
Brute Force Approach
I queried all items from my database and then iterating each record and then calculating time difference in hours which is very slow.
1) @image_retouch_items = ImageRetouchItem.where(:status => '0') = Retrieved all data
2) @image_retouch_items.each do |retouch_item|
latency_date = ((Time.parse(DateTime.now.to_s) - Time.parse(retouch_item.created_at.to_s))/3600).round
end
Optimized
I need to calculate the difference of time(hours) in query itself, how to achieve that
like - ImageRetouchItem.where(:status => '0').select('(Time.parse(DateTime.now.to_s) - Time.parse(retouch_item.created_at.to_s))/3600).round')
ruby-on-rails postgresql ruby-on-rails-3 activerecord
ruby-on-rails postgresql ruby-on-rails-3 activerecord
asked Nov 21 at 13:29
summu
4610
4610
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
Postgres can do this for you very easily using its internal current_timestamp
:
ImageRetouchItem.where(:status => '0')
.select("*, round(extract(epoch from(current_timestamp - created_at)) / 3600)::int as latency_date")
current_timestamp - created_at
will return an interval. By extracting epoch
from that interval, we convert it to a number of seconds, which we then divide by 3600 to get hours and round using the Postgres round()
function. I went ahead and casted the result as an integer using ::int
, but this is optional.
The image_retouch_item objects will now have a latency_date
attribute that will contain the latency in hours, rounded to the nearest hour.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
Postgres can do this for you very easily using its internal current_timestamp
:
ImageRetouchItem.where(:status => '0')
.select("*, round(extract(epoch from(current_timestamp - created_at)) / 3600)::int as latency_date")
current_timestamp - created_at
will return an interval. By extracting epoch
from that interval, we convert it to a number of seconds, which we then divide by 3600 to get hours and round using the Postgres round()
function. I went ahead and casted the result as an integer using ::int
, but this is optional.
The image_retouch_item objects will now have a latency_date
attribute that will contain the latency in hours, rounded to the nearest hour.
add a comment |
up vote
1
down vote
accepted
Postgres can do this for you very easily using its internal current_timestamp
:
ImageRetouchItem.where(:status => '0')
.select("*, round(extract(epoch from(current_timestamp - created_at)) / 3600)::int as latency_date")
current_timestamp - created_at
will return an interval. By extracting epoch
from that interval, we convert it to a number of seconds, which we then divide by 3600 to get hours and round using the Postgres round()
function. I went ahead and casted the result as an integer using ::int
, but this is optional.
The image_retouch_item objects will now have a latency_date
attribute that will contain the latency in hours, rounded to the nearest hour.
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
Postgres can do this for you very easily using its internal current_timestamp
:
ImageRetouchItem.where(:status => '0')
.select("*, round(extract(epoch from(current_timestamp - created_at)) / 3600)::int as latency_date")
current_timestamp - created_at
will return an interval. By extracting epoch
from that interval, we convert it to a number of seconds, which we then divide by 3600 to get hours and round using the Postgres round()
function. I went ahead and casted the result as an integer using ::int
, but this is optional.
The image_retouch_item objects will now have a latency_date
attribute that will contain the latency in hours, rounded to the nearest hour.
Postgres can do this for you very easily using its internal current_timestamp
:
ImageRetouchItem.where(:status => '0')
.select("*, round(extract(epoch from(current_timestamp - created_at)) / 3600)::int as latency_date")
current_timestamp - created_at
will return an interval. By extracting epoch
from that interval, we convert it to a number of seconds, which we then divide by 3600 to get hours and round using the Postgres round()
function. I went ahead and casted the result as an integer using ::int
, but this is optional.
The image_retouch_item objects will now have a latency_date
attribute that will contain the latency in hours, rounded to the nearest hour.
edited Nov 21 at 15:03
answered Nov 21 at 14:48
moveson
3,6021426
3,6021426
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53413130%2fcalculate-time-difference-in-rails-postgresql-query%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown