Efficient way to implement this ActiveRecord Query - mysql

I am using MySQL database and I have a datetime field inside it. In my rails application I have to fire a Query similar to the following,
MyTable.all(:conditions=>{my_date.to_date=>"2010-07-14"})
The my_date field is of datatype datetime. I should omit the time and should directly compare it with a date (but I am encountering an error near my_date.to_date due to obvious reasons). How to write an ActiveRecord Query for this scenario?
Thanks.

MyTable.all(:conditions=>["DATE_FORMAT(my_date, '%Y-%d-%m')=?", "2010-07-14"])
EDITED i think it should be
MyTable.all(:conditions=>["DATE_FORMAT(my_date, '%Y-%m-%d')=?", "2010-07-14"])

Here is an efficient solution if you index the date column:
d = "2010-07-14".to_date
MyTable.all(:conditions=>["my_date BETWEEN ? AND ?",
d.beginning_of_day, d.end_of_day)

Related

How to get time while querying in mysql?

How can I write this rails query in proper SQL query ?
time = (Time.now - 1.days).beginning_of_day
Assignment.where("created_at >= :time OR updated_at >= :time", time: time).pluck(:assignable_id).uniq
Can you help me to convert this in SQL Query ?
select distinct assignable_id from assignments where created_at >= getDate() && updated_at >= getDate();
I need help in how to get date while querying?
You can get the date using MySQL using CURDATE().
Your query would look like this:
select distinct assignable_id from assignments where created_at >= CURDATE() AND updated_at >= CURDATE();
There is no way to use inequality comparison directly using just ActiveRecord, you will have to use Arel that is a lower level api that ActiveRecord is built on. Luckily you can mix and match them most of the time.
time = (Time.now - 1.days).beginning_of_day
assignments = Assignment.arel_table
Assignment.where(
assignments[:created_at].gteq(time).or(assignments[:updated_at].gteq(time))
).distinct.pluck(:assignable_id)
Passing the current date in from ruby code is a good solution most of the time. However if you do want to use database functions, then Arel is here to help again.
time = Arel::Nodes::NamedFunction.new('getDate', [])
# or
time = Arel::Nodes::NamedFunction.new('CURDATE', [])
# or any other function you can think of
Arel does seem more complicated than just writing raw sql at first glance, but the key is reusability and safety. Arel snippets are easy to reuse by just making them into methods. It's also easy to make injection mistakes using raw sql, Arel will prevent you from making those in most™️ cases.

Convert SQLite to Django ORM

I have a query in SQLite which groups the fields by weeks and performs sum and average over distance and time fields in the same table:
select
strftime('%W', datetime) WeekNumber,
sum(distance) as TotalDistance,
sum(distance)/sum(time) as AverageSpeed
from sample_login_run
group by WeekNumber;
I am trying to convert this query to Django ORM and avoid using Raw function. I understand that I would need to use extra in Django ORM. That should not be a problem though. I came up this:
Run.objects.extra(select={'week': "strftime('%%W', datetime)"}).values(
'week','distance').annotate(
total_distance=Sum('distance'), average_time=F('distance') / F('time'))
But this is also grouping the data by average_time and average_distance field. Any help will be really appreciated. Thank you.
Right solution for this is:
Run.objects.extra(select={'week': "cast(strftime('%%W', date_run) as integer)"}).values('week').annotate(
total_distance=Sum('distance'), average_time=F('distance') / F('time'))
Fields passed in values will go under group by part of the query.

ADDTIME and SEC_TO_TIME to SQL Server 2008

I try to change my code from MYSQL to SQL Server.
I have some problems with specific functions (ADDTIME and SEC_TO_TIME) in my query.
Here is the end of my query which i have to change :
order by j.idjour,j.heure,ADDTIME(j.heure,SEC_TO_TIME(pl.duree)) asc
I tried to use convert and DateAdd but i am a bit disapointed about how to change it without any error.
Thanks for your help.
This should be what you are looking for,
dateadd(second, pl.duree, j.heure)
Assuming that pl.duree is an integer value representing seconds and that j.heure is a time or datetime value.

JPA hibernate date between query issue

In my application am using JPA entity manager to persist data/fetch data.
em.executeQuery("select * from file_calender_mapping where start_date between :start and :end");
em.setParameter("start",startDate)//startDate is an date object
em.setParameter("end",endDate)//endDate is an date object
List fmlist=em.execute();
The proble is just like this,
"select * from file_calender_mapping where start_date between start and end"
when am passing some date as start= "2011-08-03 05:08:00",and end="2011-08-04 06:08:00"
then the mysql return one row having the start time ="2011-08-03 05:30:00",its good,But
when my application executing such query it dose not returning any row.Actually what i have seen that my application returning value for two different date,but not for same date different time,thats the main problem.
One another thing is my "start" field for Table "file_calender_mapping" datatype is "timestamp".
So what i was thinking that ther may be some problem on JPA/Hibernate
You can try to specify the exact types of parameters as follows:
em.setParameter("start", startDate, TemporalType.TIMESTAMP);
em.setParameter("end",endDate, TemporalType.TIMESTAMP);
I have the strong feeling that you're confusing EntityManager.createQuery() with EntityManager.createNativeQuery() and you're somehow capturing all the exceptions, which somehow makes you don't receive anything back.
I'm assuming that, because I don't think you have a class named file_calender_mapping.
edit
The documentation will explain it better than I do, but a JPA QL query is transformed to the navite sql of the DB using the mapping, while a native query is send as it's to the DB.
Again, I suggest you to read the documentation, it's quite useful.

Mysql "Time" type gives an "ArgumentError: argument out of range" in Rails if over 24 hours

I'm writing a rails application on top of a legacy mysql db which also feeds a PHP production tool. Because of this setup so its not possible for me to change the databases structure.
The problem I'm having is that two table have a "time" attribute (duration) as long as the time is under 24:00:00 rails handles this, but as soon as rails comes across something like 39:00:34 I get this "ArgumentError: argument out of range".
I've looked into this problem and seen how rails handle the time type, and from my understanding it treats it like a datetime, so a value of 39:00:34 would throw this error.
I need some way of mapping / or changing the type cast so I don't get this error. Reading the value as a string would also be fine.
Any ideas would be most appreciated.
Cheers
I'm not familiar with Rails so there can be a clean, native solution to this, but if all else fails, one workaround might be writing into a VARCHAR field, then running a 2nd query to copy it over into a TIME field within mySQL:
INSERT INTO tablename (name, stringfield)
VALUES ("My Record", "999:02:02");
UPDATE tablename SET datefield = CAST(stringfield as TIME)
WHERE id = LAST_INSERT_ID();