I am using Parse within a Swift App.
How do get the SlotName and appropriate day field(s) on records where Monday, Tuesday, Wednesday, Thursday and/or Friday equals "username".
Could you do this in three steps:
Retrieve all records where:
Monday = Username
Tuesday = Username
etc.
Then check to see what the SlotName is for the given record.
Then for the returned record, iterate through to see if:
Monday = Username
Tuesday = Username
etc.
Where a record's value for a given week day is True (for example: Monday = Username), then put this into an array. This would give you a list of appointments and the times of the said appointments for a given username.
Related
Informix-SE 4.10 with isql 4.10 DD6:
I have a DATE column, BirthDate, linked to a perform field tag "dob". When users enter the dob, I am doing the following date arithmetic:
age = (TODAY - dob ) / 365.25
field tag age is DISPLAYONLY TYPE DECIMAL field, with FORMAT="##.##".
This method is not producing accurate results.
Business Rule: Example, If a person true age is not 18 years old the day before their 18th birthday, I need to display 17 in the age field tag, and abort insertion of the customer row. Person must be >=18 years old.
DATETIME and INTERVAL datatypes are supported in this ifx version, but users are only comfortable entering dates in MMDDYYYY format.
In order to determine if the birthday has happened this year, you must be sure not to generate a non-valid date in the case where dob is February 29 and TODAY is in a non leap-year. Since 2000 is a leap-year, you can avoid the problem making the subtraction always for the year 2000.
MDY(MONTH(dob), DAY(dob), 2000) > MDY(MONTH(TODAY), DAY(TODAY), 2000)
Finally, subtract one from the year difference if so:
age = (YEAR(TODAY) - YEAR(dob) -
CASE WHEN MDY(MONTH(dob), DAY(dob), 2000) > MDY(MONTH(TODAY), DAY(TODAY), 2000)
THEN 1 ELSE 0 END)::int
In my app users save a report each Friday for an organization. All my records in the reports table hold organization_id, day and workdays.
I want to get the sum of all workdays at the last Friday of each month.
With the following line I am able to select all reports for organization #8 and sum workdays for each Friday:
#all_report_sums = Report.where(:organization_id => 8).group(:day).select("day, SUM(workdays) AS sum_workdays")
The result:
[{"id":null,"day":"2017-02-03","sum_workdays":3},{"id":null,"day":"2017-02-24","sum_workdays":33},{"id":null,"day":"2017-04-07","sum_workdays":12}]
However in my output I only want the maximum dates of each month (which is the last Friday of each month) - so in my case this would be 2017-02-24 and 2017-04-07.
How can I achieve that?
This is what are you looking for
How to get the latest record from each group in ActiveRecord?
User.group(:user_id).having('day = MAX(day)')
There are two tables:
1. users - {id, name, ...}
2. users_availabiltiy {id, user_id, monday, tuesday, wednesday, thursday, friday, saturday, sunday, ...}
[moday, truesday and other days fields are of boolean type]
So, if a user is available for monday, wednesday and thursday, that will have true as its value and the rest will have value as false.
and there is has_one relation between these two tables.
Now, the problem is, I need a single ActiveRecord query to get all those users which are available on n days [user inputs the days for which users are returned, e.g. if user enter monday, thursday and friday, our system needs to return all those users which are available for any of these days]
Plus we also need to order the result set based on maximum days matching.
[all days matches -> first ranking]
[all days matches-1 -> second ranking] and so on.
Note: Database is postgresql and rails version is 4.
This is the easiest approach:
input = ["moday","tuesday", "wednesday"]
b = input.map{|day| day+" = true"}
c = b.join(" or ")
b1 = a.map{|day| day+"::int"}
c1 = "*, "+b1.join(" + ")+" as total"
User.joins(:user_availabilty).select(c1).where(c).order("total DESC")
Can someone let me know the best and optimized approach apart from this?
This is the easiest approach:
input = ["moday","tuesday", "wednesday"]
b = input.map{|day| day+" = true"}
c = b.join(" or ")
b1 = a.map{|day| day+"::int"} c1 = "*, "+b1.join(" + ")+" as total"
User.joins(:user_availabilty).select(c1).where(c).order("total DESC")
Can some one let me know the best and optimized approach apart from this.
Question:
What's the easiest way to take a DateTime object "A" and apply the day of week and time of another DateTime object "B" so that I have a new DateTime object that's in the week of DateTime object "A" but has the day of week and time of DateTime object "B"?
Some context:
I'm creating a scheduling app where users have recurring appointments. All users set their default day of week and time in form of a DateTime object (I save it as a DateTime but actually I don't care about the date itself).
When I want to extend their recurring appointments (e.g. for another 6 months), I need to get the week of their last appointment, jump to the default day of week and time within that week and extend from there.
require 'date'
def new_date_time(a,b)
a_date = a.to_date
new_date = a_date + (b.to_date.wday - a_date.wday)
DateTime.new(new_date.year, new_date.month, new_date.day,
b.hour, b.minute, b.second)
end
a = DateTime.new(2015,8,6,4,5,6) # Thursday
#=> #<DateTime: 2015-08-06T04:05:06+00:00 ((2457241j,14706s,0n),+0s,2299161j)>
b = DateTime.new(2015,8,3,13,21,16) # Monday
new_date_time(a,b)
#=> #<DateTime: 2015-08-03T13:21:16+00:00 ((2457238j,48076s,0n),+0s,2299161j)>
b = DateTime.new(2015,11,17,13,21,16) # Tuesday
new_date_time(a,b)
#=> #<DateTime: 2015-08-04T13:21:16+00:00 ((2457239j,48076s,0n),+0s,2299161j)>
Using Rails 4, I have a set of data where I only want the data points where the date is on the last day of the month, any month, but only where the date is the last day of the month.
Below I set up the #data variable all my statistics data limiting it to Statistics where the date is in the last year. I would also like to limit the data to only contain Statistics that have a date of the last day of the month.
EDIT
- Note that the start date is a variable and can be changed by a parameter
startdate = Date.1.year.ago
#data = Statistic.where(date: startdate..Date.today).all
Is there a simple way to add another .where clause and use end_of_month for this?
Write this scope in your Statistics model:
scope :last_date_stats, ->(dates) { where(date: dates) }
Populate a dates array in your StatisticsController and then use the scoped query to get specific data:
dates = (0..11).to_a.map { |n| n.months.ago.end_of_month }
#last_date_statistics = Statistics.last_date_stats(dates)