Query working days from calendar in CRM 2015 - sql

I have created a calendar (working days) for a resource (e.g. facility). How can I get a list or at least ranges of working days directly from the database?
All I have found so far are Calendar and CalendarRule views. The problem with the CalendarRule is that the rule is written with a pattern like
"FREQ=WEEKLY;INTERVAL=1;BYDAY=MO,TU,WE,TH,FR"
I can not find a way to use this in SQL query.

Assuming working days are mon to fri and your calendar table is linked to the resources table by calendar.workingdays = resources.daysworked, if not obviously adjust them to suit.
SELECT EXTRACT(dow FROM timestamp (to_timestamp(c.workingdays), "YYYY-MM-DD")), r.facility
FROM Calendar c
LEFT JOIN Resources r ON c.workingdays = r.daysworked
WHERE EXTRACT(dow FROM timestamp (to_timestamp(c.workingdays, "YYYY-MM-DD"))
NOT IN (0,6)

Related

Elasticsearch SQL - How to query data only from yesterday

I am looking to query data from yesterday, excluding the data from today. I have figured out how to do this when inputting a specific date and subtracting 1 day. However, I need it to be from today's date, for example timestamp = NOW() - 1d/d. This is on an element in Kibana Canvas, using Elasticsearch SQL.
This is what I have so far, however I would need to update the query everyday to change the specific date.
SELECT COUNT(XXX.keyword) AS Count
FROM "XXX"
WHERE XXX.keyword='XXX'
AND timestamp = '2022-03-09||-1d/d'
timestamp = NOW() - INTERVAL 2 DAYS would not work because it includes data from TODAY and YESTERDAY, whereas I only want the data from yesterday.
Thank you.
It looks like you want curdate() or current_date(). Have a look at the link below for more extensive info on how to query dates (relative and otherwise).
https://www.elastic.co/guide/en/elasticsearch/reference/current/sql-functions-datetime.html

Recurring Date Calculator SQL

I'm looking to create a recurring date calculator that will correspond to EVENTS on a specific calendar date. Each event has its own rule.
For example, for EVENT A rule is "Occurs Tuesday monthly with the start date Tuesday 9-17-2019. I'm using a time dimension table with every date populated until 2025. Using these dates I was using in the WHERE clause
WHERE dayname = 'tuesday' and ( DATEDIFF(DAY, '2019-09-17', Calendar_Date) % 28 ) = 0 to get monthly tuesdays.
But I'm running into issues when there are two 5 Tuesdays in a month. 3-3-2020 and 3-31-2020. Need date populated to be 4-7-2020 instead of 3-31-2020.
Can anyone help with a solution?
With your query, you're pretty close. All you need to do is to then sub-select
SELECT MIN(CalendarDate) AS CalendarDate
FROM (your query goes here) AS d
GROUP BY YEAR(CalendarDate), MONTH(CalendarDate);

How to calculate a birthday date regardeless of year

I would like to calculate customer with the account creation date in the next 8 days, so i want to calculate this date using only the day and the month , if the creation anniversary date is in 8 days, i can retrieves these contacts and contact them.
is there any exemple of query using only month an day to calculate the upcoming date?
thank you
You can try doing something like this (tested on PostgresQL, may require some adjustments for other DBs):
select * from temp_contacts where
to_char(date_created, 'MM-dd') =
to_char(current_date + interval '8 day', 'MM-dd');
For each row it first maps the date_created to a string using MM-dd format, then does the same to the current date + 8 days and compares the two.
This of course is not using any index, so it will not perform well on a table with large enough size.
Some DBs support creating function based indices. For example PostgresQL or Oracle DB. In MySQL you can achieve the same by creating an auto generated column and indexing on it.

Checking if any date between date range was within a specific year

I am racking my brain on this one. I have a list of campaigns and their associated start and end dates. I'm trying to write a query to list the campaigns where at least one day between the start and end date was in 2017. My original thought was just use datepart():
datepart(yyyy,StartDt = 2017) OR datepart(yyyy,EndDt = 2017)
But that wont pick up a campaign that started before 2017 and ended after 2017, like start date 1/1/2016 and end date 1/1/2018.
Probably the easiest way to do this is like this (it's something I use when having to do temporal table queries):
WHERE CampaignStartDate < '2018-01-01'
AND CampaignEndDate >= '2017-01-01'
This guarantees that your campaign start and end dates had at least some overlap with the 2017 year. It's a good way of testing if two time ranges overlap at all.

SQL query to search by day/month/year/day&month/day&year etc

I have a PostgreSQL database with events. Each event has a datetime or an interval. Common data are stored in the events table and dates are stored in either events_dates (datetime field) or events_intervals (starts_date, ends_date both are date fields).
Sample datetime events
I was born on 1930-06-09
I got my driver's license on 1950-07-12
Christmas is on 1900-12-24 (1900 is reserved for yearly reoccuring events)
Sample interval events
I'll be on vacation from 2011-06-09 till 2011-07-23
Now I have a user that will want to look up these events. They will be able to fill out a form with from and to fields and in those fields they can enter full date, day, month, year, day and month, day and year, month and year in one or both fields.
Sample queries
From May 3 to 2012 December 21 will look for events between May 3 and December 21 whose max year is 2012
From day 3 to day 15 will look for events between the 3rd and 15th day of every month and year
From day 3 will look for events on the 3rd day of every month and year (same if from is empty and to is not)
From May 3 to June will look for events between May 3 and last day of June of every year
etc.
Any tips on how to write a maintanable query (it doesn't necessarily have to be fast)?
Some things that we thought of
write all possible from, to and day/month/year combinations - not maintable
compare dates as strings e.g. input: ____-06-__ where _ is a wildcard - I wouldn't have to generate all possible combinations but this doesn't work for intervals
You can write maintainable queries that additionally are fast by using the pg/temporal extension:
https://github.com/jeff-davis/PostgreSQL-Temporal
create index on events using gist(period(start_date, end_date));
select *
from events
where period(start_date, end_date) #> :date;
select *
from events
where period(start_date, end_date) && period(:start, :end);
You can even use it to disallow overlaps as a table constraint:
alter table events
add constraint overlap_excl
exclude using gist(period(start_date, end_date) WITH &&);
write all possible from, to and day/month/year combinations - not maintable
It's actually more maintainable than you might think, e.g.:
select *
from events
join generate_series(:start_date, :end_date, :interval) as datetime
on start_date <= datetime and datetime < end_date;
But it's much better to use the above-mentioned period type.