Getting nth user based on created_at date - ruby-on-rails

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;

Related

Getting time-stamp difference of same user coming multiple times on a website

I have data in BigQuery which have specific columns like time-stamp and userid, Some users visit the website multiple times.
The goal is to find out the time difference of users visiting multiple times.
Even if they visit 14 times, I need to find the difference between every consecutive visit.
This is a sample of my data:
This should help (assuming you want delta in minute). You can always switch to whatever period you need (hour, second, etc.)
Please note the usage of analytical function LAG which uses data partitioned over user_id and ordered by timestamp ts. Also, note that the first appearance of the user_id gets the difference of 0 because this is the first time user showed up :). Hope it helps.
select user_id, coalesce(timestamp_diff(ts_a, ts_b, minute), 0) as diff_from_prv_visit_minutes from (
select user_id, ts as ts_a, lag(ts) over (partition by user_id order by ts) as ts_b
from `mydataset.mytable`
)

better design for fact table where each row has a Start & End Date

My fact table contains details for clients who attend a course.
To ensure i can get a list of clients registered on any particular day, I have not related the date dimension to the fact table.
Instead i created a measure that does basic between logic (where startDate <= selectedDate && endDate >=SelectedDate)
This allows me to find all clients registered on one single selected day.
There are a few drawback to this however:
-I have to ensure the report user only selects a single day, i.e. they cannot select a date range.
-I cant easily do counts for samePeriodLastMonth or Year.
Is there a better design i should consider that will still allow me to see counts of registered clients on any given day, along with allowing me to use SamePeriodLastMonth/Year functionality?
Would you mind uploading the structure of your fact and dim tables?
Just a thought bubble: if you would like to measure counts for a program over calendar years, I believe you would definitely need to create a Date dimension. Also depending on your reporting needs you might want to consider whether you need an Accumulating Snapshot Fact table.
Please find further details on this:
http://www.kimballgroup.com/2012/05/design-tip-145-time-stamping-accumulating-snapshot-fact-tables/
Cheers
Nithin

How to check if there are blank period of posts

I want to check if there was a blank period that have no posts.
I wrote like this:
Post.all.pluck(:date).sort.each_cons(2).any?{|i,j| (i - j).abs > 7 }
Is there a better implementation?
I'm not good at database functions. Is there a function for it in Postgresql.
Albeit expensive, you could do a cross join (which really is a cartesian join) and calculate directly in the database. Also, here I assume that the columns are Datetime fields instead of Date.
For MySQL
Post.
joins('CROSS JOIN posts p2').
where('TIMESTAMPDIFF(DAY, date, p2.date) > 7').
present?
Note that the difference needed to be converted to days if you want 7 days period.
For Postgres, do check out the relevant function at the postgres datetime docs. You might have to do something like this:
EXTRACT(DAY FROM ABS(d1 - d2))

Rails - find record with highest month/year

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

order by created_at plus another value

I have a table which contains a column "discussion_duration" where I am storing certain times in seconds (6 hours = 21600). I would like to sort this table by 'created_at' + 'discussion_duration'.
I can add created_at and discussion_duration in ruby and it returns a date. However since the parameters in the rails .order() is raw sql, I'm not sure how to add these two fields then sort by the output.
Any help would be much appreciated.
edit: Actually this is probably what you're looking for. You'll need to use your database specific date/time functions to do it and here's a mysql example.
.order('TIMESTAMPADD(SECOND, discussion_duration, created_at)')

Resources