I'm kinda stuck in a select query question:
I have a Bill model, which contains two integer attributes: month and year. I would like to retrieve the more recent record (highest date) so I can check an attribute value on it. Any ideas for solving that problem, since month and year are independent attributes?
Thanks!
Bill.order('year DESC, month DESC').first
Related
I collect customer feedback for my education business and add it to a Google Sheet. The feedback data has a submission date (A2:A) and some satisfaction metrics, which I visualize in a Google Data Studio dashboard.
The problem is that I want the feedback per cohort, but not everyone fills in the feedback form on the same day. I have a list of all courses with their respective dates (Cohorts!A2:A), and I want to assign each feedback submission to their respective cohort in a new column. It would be nice to also match it to the specific course type and country, but for now matching the cohort date would suffice.
I've tried using VLOOKUP and ARRAYFORMULA to go through the feedback dates and get the nearest past date to take it as the "course date" for that student. All the solutions I've tried either only take a single date or TODAY as a reference, but I have a whole list I'd like to fill in.
From my understanding, you are trying to round the timestamp, then match it to your course table?
To round a timestamp to a date:
=INT($A2)
When doing lookups like you're describing, I frequently end up calculating the nearest week as well - this formula returns the Sunday of the week start. Figured it might be helpful.
=text($A2+CHOOSE(WEEKDAY($A2),0,-1,-2,-3,-4,-5,-6),"m/d/yyyy")
I have a query regarding sorting by date, I can easily sort the data I have based on a field by adding
.order('search_date desc')
Search date is the date in full - dd/mm/yyyy, but I do not want to add the year to this, I only want to order by DD/MM.
Does anyone know the best way to do this?
It's more a sql solution than a rails solution :
If you are using mysql :
.order("DATE_FORMAT(search_date, '%d%m')")
will sort by day and month
My initial thought was:
user.humans.where("created_at = ?", 10.days.ago)
Though, this seems to be looking for the record created 10 days ago at the exact time when the statement is called. I want to collect the records created on that day, regardless of their time.
Is anyone aware of a convenient way to do this? Let me know if I need to elaborate.
Thanks.
You'll probably want to use a range here, as I assume this is a datetime column.
User.humans.where("? <= created_at AND created_at <= ?", 10.days.ago.beginning_of_day, 10.days.ago.end_of_day)
You'll also want to make sure you're setting the time zone of your Rails application so that you're explicit about which time period you consider to be the 10th day.
Whichever DBMS you are using will have a method to convert a datetime to a date. You should then compare this to a date in ruby. For example, if your DBMS is MySQL you could say
user.humans.where("date(created_at) = ?", 10.days.ago.to_date)
If you're not using MySQL then you should be able to google converting a datetime to a date in your DBMS of choice.
Original question:
Let's say I have 10 users in my database, and I want to get the 6th (or 3rd, or 8th, whatever) user based on sorted created_at date. How can I do this?
Edited question:
Let's say I have 10 users in my database, and I have a certain user, how can I determine wich nth user it is if I sort them on created_at date? (so user X is the nth user of all user based on created_at)
Sorry about the confusion!
This should work: (obviously have to change the offset value as needed)
User.order(:created_at).limit(1).offset(6)
Ok, the edited question makes this a bit more complex. Not sure if ActiveRecord is the right thing to use here. Some databases haw a kind of virtual "rownumber" which you could use for this. But lets assume we don't have that. SQL can't do that natively as such, so you could only count the number of records that are 'less' than what you look for.
Assume we have found the relevant User as user.
Then we could do:
User.where("created_at < :created_at", created_at: user.created_at).count
At least the best I can come up with.
User.order("created_at DESC").limit(1).offset(6)
or
User.order("created_at DESC").limit(1).offset(6)
It's work like :
select * from users order by created_at DESC LIMIT 5 OFFSET 30;
I am having problem in calculating number of working days for particular month. I have attendance model.which contains, school_id,course_id,section_id,student_id,attendance_date attributes.These fields for making attendance for particular section in particular school
For viewing attendance report, I have school,course,section and month fields.when I select the month it will show the number of working days for that particular month.So, I have to calculate working days by attendance_date field from attendance model. I am having confusion in that. Please help me.
You might want to take a look at business_time
Example:
4.business_days.from_now
8.business_days.after(some_date)