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.
Related
I've been trying to get a list of people who finish their work shift at an specific our, I have not had any problem with this since I've used just a query function:
=QUERY(A1:K48,"select A where B = '7:00 AM' OR C = 'Sunday' OR D = 'Sunday' OR K = TRUE",0)
So, if someone ends their work shift at 7:00 AM or someone rests on Sunday the formula will return these people's names.
Unfortunately, I'm struggling with the next step since those workers work an extra hour or half extra hour some specific days and I was not be able to include in the formula.
For instance, if today is Sunday and today is the Sean long day in which he has to work one extra hour I would like to this Query Formula doesn't includes the Sean's name at 7:00 AM instead of it I would like to include the Sean's name until 8:00 AM. To do this I add "not" in the query formula:
=QUERY(A1:K48,"select A where B = '7:00 AM' AND NOT E matches 'Sunday' OR C = 'Sunday' OR D = 'Sunday' OR K = TRUE",0)
To indicate that Sean is still working, and the formula shouldn't include his name, but now I don't know how to get Sean's name at 8:00 AM.
This is the sheet where I'm working on this Query: Sheet
https://docs.google.com/spreadsheets/d/1Vk95yM8On5nLlFljFerzDH4IG8dZkOgKyHzX36uUWOQ/edit?usp=sharing
To get a clearer idea: I'm getting a list based on column B, C and D values(normal shift days) but before of getting this result I would like to based the list on column E and column F data (long day and hour of the long day) and then if there is no data to take from the E and F column run the first formula.
I hope someone could help me with this I'll be so obliged. Thank you!
I'm trying to iterate over each week in the calendar year and run a query.
range = Date.new(2020,3,16)..Date.new(2020,3,22)
u = User.where(created_at: range).count
But I'd like to do this for EACH week in another range (say since the beginning of this year).
Ruby's Date has a cweek function that gives you the week number but there doesn't seem to be a way to easily get from the week number to the date range.
Anyway, not sure how helpful cweek will be as I need week to run Sunday -> Saturday.
Thoughts?
I'm assuming this is Postgres and the model name is User based on your previous question.
If this blog is to to believed you can shift a date one day to get sun-sat day week.
User.group("(date_trunc('week', created_at::date + 1)::date - 1)")
.count
If you want the to select the actual week number while you are at it you can select raw data from the database instead of using ActiveRecord::Calculations#count which is pretty limited.
class User
# #return [ActiveRecord::Result]
# the raw query results with the columns count, year, week
def self.count_by_biblical_week
connection.select_all(
select(
"count(*) as count",
"date_part('year', created_at)::integer as year",
"(date_part('week', created_at::date + 1) - 1)::integer as week"
).group(:week, :year)
)
end
end
Usage:
results = User.where(created_at: Date.new(2020,3,16)..Date.new(2020,3,22))
.count_by_biblical_week
results.each do |row|
puts [row[:year], row[:week], row[:count]].join(' | ')
end
Adding the year to the group avoids ambiguity if the results span multiple years.
I have an array formula that's not working exactly as I want.
The formula I have does not return correct value (returns FALSE) when leave dates are in the same month and month = current month
The formula is supposed to check for these conditions:
Col. E = Annual Leave
Col. J = Complete
and return the number of leave days taken by an employee for each month as per these conditions:
(let's assume Current month = Feb-2019)
a) IF Col. O = Yes AND Current month = Leave end date (means start date and end date are in the same month = current month)
THEN return number of leave days taken
b) IF Col. P = Yes (leave dates start in one month and end in another month)
(i) IF Current month = Leave start date THEN return number of days between leave start date and end of start date month
(ii) IF Current month = Leave end date THEN return total days taken (Col. H) minus number of days between leave start date and end of start date month
=ArrayFormula(iferror(if(($J$2:$J<>"Complete")*($E$2:$E<>"Annual Leave"),"",if($P$2:$P="yes",if(month($F$2:$F)=month(R$1),NETWORKDAYS.INTL($F$2:$F,EOMONTH($F$2:$F,0),1,Holidays!$B$2:$B),if(month($G$2:$G)=month(R$1),$H$2:$H-NETWORKDAYS.INTL($F$2:$F,EOMONTH($F$2:$F,0),1,Holidays!$B$2:$B),if(month($G$2:$G)=month(R$1),if($O$2:$O="Yes",$H$2:$H,0)))))),"x"))
The formula I have does not return correct value (returns FALSE) when leave dates are in the same month and month = current month
All other conditions are OK (i.e. if True, all other conditions are executed properly and correct value displayed except the above condition)
Link to google sheet
I've done some more research - and also based on the suggestion - I have managed to resolve this problem.
Here's the working formula:
=ArrayFormula(iferror(if((($J$2:$J="Complete")*($E$2:$E="Annual Leave")),
IF($P$2:$P="Yes",
IF(month($F$2:$F)=month(R$1),NETWORKDAYS.INTL($F$2:$F,EOMONTH($F$2:$F,0),1,Holidays!$B$2:$B),
IF(month($G$2:$G)=month(R$1),$H$2:$H-NETWORKDAYS.INTL($F$2:$F,EOMONTH($F$2:$F,0),1,Holidays!$B$2:$B),
IF(month($G$2:$G)=month(R$1)))),
IF((($O$2:$O="Yes")*(month($F$2:$F)=month(R$1))),$H$2:$H,"")),""),""))
you have 3 incomplete IFs which return falses...
=ARRAYFORMULA(IFERROR(
IF(($J$2:$J="Complete")*($E$2:$E="Annual Leave"),
IF($P$2:$P="yes",
IF(MONTH($F$2:$F)=MONTH(S$1), NETWORKDAYS.INTL($F$2:$F, EOMONTH($F$2:$F, 0), 1, Holidays!$B$2:$B),
IF(MONTH($G$2:$G)=MONTH(S$1), $H$2:$H-NETWORKDAYS.INTL($F$2:$F, EOMONTH($F$2:$F, 0), 1, Holidays!$B$2:$B),
IF(MONTH($G$2:$G)=MONTH(S$1),
IF($O$2:$O="Yes", $H$2:$H, 0), ))), ), ), ))
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.
I have a Ruby on Rails application with a PostgreSQL database; several tables have created_at and updated_at timestamp attributes. When displayed, those dates are formatted in the user's locale; for example, the timestamp 2009-10-15 16:30:00.435 becomes the string 15.10.2009 - 16:30 (the date format for this example being dd.mm.yyyy - hh.mm).
The requirement is that the user must be able to search for records by date, as if they were strings formatted in the current locale. For example, searching for 15.10.2009 would return records with dates on October 15th 2009, searching for 15.10 would return records with dates on October 15th of any year, searching for 15 would return all dates that match 15 (be it day, month or year). Since the user can use any part of a date as a search term, it cannot be converted to a date/timestamp for comparison.
One (slow) way would be to retrieve all records, format the dates, and perform the search on that. This could be sped up by retrieving only the id and dates at first, performing the search, and then fetching the data for the matching records; but it could still be slow for large numbers of rows.
Another (not database-agnostic) way would be to cast/format the dates to the right format in the database with PostgreSQL functions or operators, and have the database do the matching (with the PostgreSQL regexp operators or whatnot).
Is there a way to do this efficiently (without fetching all rows) in a database-agnostic way? Or do you think I am going in the wrong direction and should approach the problem differently?
Building on the answer from Carlos, this should allow all of your searches without full table scans if you have indexes on all the date and date part fields. Function-based indexes would be better for the date part columns, but I'm not using them since this should not be database-specific.
CREATE TABLE mytable (
col1 varchar(10),
-- ...
inserted_at timestamp,
updated_at timestamp);
INSERT INTO mytable
VALUES
('a', '2010-01-02', NULL),
('b', '2009-01-02', '2010-01-03'),
('c', '2009-11-12', NULL),
('d', '2008-03-31', '2009-04-18');
ALTER TABLE mytable
ADD inserted_at_month integer,
ADD inserted_at_day integer,
ADD updated_at_month integer,
ADD updated_at_day integer;
-- you will have to find your own way to maintain these values...
UPDATE mytable
SET
inserted_at_month = date_part('month', inserted_at),
inserted_at_day = date_part('day', inserted_at),
updated_at_month = date_part('month', updated_at),
updated_at_day = date_part('day', updated_at);
If the user enters only Year use WHERE Date BETWEEN 'YYYY-01-01' AND 'YYYY-12-31'
SELECT *
FROM mytable
WHERE
inserted_at BETWEEN '2010-01-01' AND '2010-12-31'
OR updated_at BETWEEN '2010-01-01' AND '2010-12-31';
If the user enters Year and Month use WHERE Date BETWEEN 'YYYY-MM-01' AND 'YYYY-MM-31' (may need adjustment for 30/29/28)
SELECT *
FROM mytable
WHERE
inserted_at BETWEEN '2010-01-01' AND '2010-01-31'
OR updated_at BETWEEN '2010-01-01' AND '2010-01-31';
If the user enters the three values use SELECT .... WHERE Date = 'YYYY-MM-DD'
SELECT *
FROM mytable
WHERE
inserted_at = '2009-11-12'
OR updated_at = '2009-11-12';
If the user enters Month and Day
SELECT *
FROM mytable
WHERE
inserted_at_month = 3
OR inserted_at_day = 31
OR updated_at_month = 3
OR updated_at_day = 31;
If the user enters Month or Day (you could optimize to not check values > 12 as a month)
SELECT *
FROM mytable
WHERE
inserted_at_month = 12
OR inserted_at_day = 12
OR updated_at_month = 12
OR updated_at_day = 12;
"Database agnostic way" is usually a synonym for "slow way", so the solutions will unlikely be efficient.
Parsing all records on the client side would be the least efficient solution in any case.
You can process your locale string on the client side and form a correct condition for a LIKE, RLIKE or REGEXP_SUBSRT operator. The client side of course should be aware of the database the system uses.
Then you should apply the operator to a string formed according to the locale with database-specific formatting function, like this (in Oracle):
SELECT *
FROM mytable
WHERE TO_CHAR(mydate, 'dd.mm.yyyy - hh24.mi') LIKE '15\.10'
More efficient way (that works only in PostgreSQL, though) would be creating a GIN index on the individual dateparts:
CREATE INDEX ix_dates_parts
ON dates
USING GIN
(
(ARRAY
[
DATE_PART('year', date)::INTEGER,
DATE_PART('month', date)::INTEGER,
DATE_PART('day', date)::INTEGER,
DATE_PART('hour', date)::INTEGER,
DATE_PART('minute', date)::INTEGER,
DATE_PART('second', date)::INTEGER
]
)
)
and use it in a query:
SELECT *
FROM dates
WHERE ARRAY[11, 19, 2010] <# (ARRAY
[
DATE_PART('year', date)::INTEGER,
DATE_PART('month', date)::INTEGER,
DATE_PART('day', date)::INTEGER,
DATE_PART('hour', date)::INTEGER,
DATE_PART('minute', date)::INTEGER,
DATE_PART('second', date)::INTEGER
]
)
LIMIT 10
This will select records, having all three numbers (1, 2 and 2010) in any of the dateparts: like, all records of Novemer 19 2010 plus all records of 19:11 in 2010, etc.
Watever the user enters, you should extract three values: Year, Month and Day, using his locale as a guide. Some values may be empty.
If the user enters only Year use WHERE Date BETWEEN 'YYYY-01-01' AND 'YYYY-12-31'
If the user enters Year and Month use WHERE Date BETWEEN 'YYYY-MM-01' AND 'YYYY-MM-31' (may need adjustment for 30/29/28)
If the user enters the three values use SELECT .... WHERE Date = 'YYYY-MM-DD'
If the user enters Month and Day, you'll have to use the 'slow' way
IMHO, the short answer is No. But definitely avoid loading all rows.
Few notes:
if you had only simple queries for exact dates or ranges, I would recommend using ISO format for DATE (YYYY-MM-DD, ex: 2010-02-01) or DATETIME. But since you seem to need queries like "all years for October 15th", you need custom queries anyways.
I suggest you create a "parser" that takes your date query and gives you the part of the SQL WHERE clause. I am certain that you will end up having less then a dozen of cases, so you can have optimal WHEREs for each of them. This way you will avoid loading all records.
you definitely do not want to do anything locale specific in the SQL. Therefore convert local to some standard in the non-SQL code, then use it to perform your query (basically separate localization/globalization and the query execution)
Then you can optimize. If you see that you have a lot of query just for year, you might create a COMPUTED COLUMN which would contain only the YEAR and have index on it.