Oracle: counting sum of flags between two dates - sql

I have two tables:
The first one contains the beginning and the end of an interval:
------------------------------
DATE_START | DATE_END
------------------------------
2020-01-04 2023-07-04
------------------------------
The second one is the calendar with dates and boolean "special day" indicator:
------------------------------
DATE | SPECIAL_DAY_FLAG
------------------------------
2020-01-04 1
------------------------------
2020-01-05 0
------------------------------
So I need a query, that returns only rows from the first table, where number of "special days" between DATE_START and DATE_END is greater than 14.

You can join to your second table (which I've called 'calendar') based on the date range from the first table (which I've called 'intervals'); filter those joined records on the flag value, and count how many matches there are; and finally use having to check the count is 14 or more:
select i.date_start, i.date_end,
count(c.calendar_date) as special_days
from intervals i
join calendar c on c.calendar_date >= i.date_start and c.calendar_date <= i.date_end
where c.special_day_flag = 1
group by i.date_start, i.date_end
having count(c.calendar_date) >= 14
fiddle
... where I've assumed the interval is inclusive - if it isn't then change to < end_date.
If you wanted to count total matched days as well as special days you could also use conditional aggregation; which would need to be in a subquery to then filter by number of special days.

Related

Compare date filed with month and year in Postgres

I have a date field in one of my tables and the column name is from_dt. Now I have to compare a month and year combination against this from_dt field and check whether the month has already passed. The current database function uses separate conditions for the month and the year, but this is wrong as it will compare month and year separately. The current code is like this
SELECT bill_rate, currency FROM table_name WHERE
emp_id = employee_id_param
AND EXTRACT(MONTH FROM from_dt) <= month_param
AND EXTRACT(YEAR FROM from_dt) <= year_param
Now the fromt_dt field has value 2021-10-11. If I give month_param as 01 and year_param as 2022, this condition will not work as the month 10 is greater than 1, which I have given. Basically, I need to check whether 01-2022 (Jan 2022) is greater than r equal to 2021-10-01(October 1st, 2021). It would be very much helpful if someone can shed some light here.
If you just want to check whether one date is >= then another:
# select '2022-01-01'::date >= '2021-10-11'::date;
?column?
----------
t
If you want to restrict to year/month then:
select date_trunc('month','2022-01-01'::date) >= date_trunc('month', '2021-10-11'::date);
?column?
----------
t
Where the date_trunc components are:
select date_trunc('month','2022-01-01'::date) ;
date_trunc
------------------------
2022-01-01 00:00:00-08
select date_trunc('month','2021-10-11'::date) ;
date_trunc
------------------------
2021-10-01 00:00:00-07
See Postgres date_trunc for more information.
Assuming the given year_param and month_param are integers you can use the make_date function to create the first of the year_month and date_trunc to get the first on the month from the table. Just compare those values. (See date functions) So:
select bill_rate, currency
from table_name
where emp_id = employee_id_param
and date_trunc('month',from_dt) =
make_date( year_param, month_param, 01);

SQL - Select query not displaying all dates

I have a table that has a start date, an end date, and the pay period information according to the start and end dates. When I try to find the pay period information with a date range, the very first pay period information does not show in the result.
For example, when I run the following query:
select *
FROM PayPeriod
where start_date between '2020-12-01' and '2020-12-21'
I should see the following result:
Start_date End_date Pay_perild
2020-11-22 2020-12-05 2020-12-wk1
2020-12-06 2020-12-19 2020-12-wk3
2020-12-20 2021-01-02 2021-01-wk1
Instead, I get:
Start_date End_date Pay_period
2020-12-06 2020-12-19 2020-12-wk3
2020-12-20 2021-01-02 2021-01-wk1
The date range and the pay period that includes '2020-12-01' is omitted. Why isn't it showing, and how do I correct this?
Looks like I've got what you wanted. You need to intersect two time intervals.
To find intersecting intervals of two tables (say TableA and TableB, I use tables as more general case to distinguish two intervals by their meaning/role/whatever) you need to compare begin date of one table with end date of another for both tables (putting each of them to "one" and "another" role):
TableA.start_date < TableB.end_date
and TableB.start_date < TableA.end_date
It is the rule for the case where your intervals are continuous, e.g. end_date of one period is "just before" (like real numbers) of the start_dare of another (so all items in the interval will have start_date <= item_date < end_date). For discrete intervals (like days, where duration of one day will have identical values of start_date and end_date) there would be <= in intersection condition.
So, your query will look like
DECLARE #period_from date = CONVERT ('2020-12-01' 23),
#period_to date = CONVERT ('2020-12-21', 23);
select *
FROM PayPeriod
where start_date < #period_to /*or <= depending on inclusion of end_date*/
and #period_from < end_date /*or <= depending on inclusion of end_date*/
The query is returning the result as you instructed. It's working perfectly. In your query you put the date range condition one the start_date:
where start_date between '2020-12-01' and '2020-12-21'
The first row that you expect has start_date = 2020-11-22. This date is not in the rage you specified in the condition.
If you want the first row in the result set simply you need to change the condition.
If you have to put condition on the start date you have to make the date range wider in the condition. For example:
SELECT *
FROM PayPeriod
WHERE start_date between '2020-11-21' and '2020-12-21'
Based on your application requirement you have to arrange the condition.

How to query checking if a month is between two dates?

I'm looking for query in postgresql to check whether a month is between two dates or not.
I know how to check if a date is between two date or not. Postgres also have a function to do that.
Let's say I have a a_table with rows:
ID | start_date (timestamp) | end_date (timestamp)
1 | 2019-07-20 00:00 | 2020-03-20 00:00
2 | 2019-08-20 00:00 | 2020-08-30 00:00
I have a to return the row that include a month between the start_date and end_date.
Let's say i have a month 2019-08.
So when i count
Select count(*) from a_table
Where [some where clause]
it returns 2 rows, ID 1 and ID 2
AND when i have a month 2020-01 it only return ID 1
You can use date range for this.
It's not clear to me what should happen if the start/end date in the table only covers part of a month.
If you only want to consider the full month, use the "contains" operator
select count(*)
from the_table
where daterange(start_date::date, end_date::date, '[]') #> daterange('2019-08-01'::date, '2019-09-01'::date, '[)');
The <# is the "is contained" operator which tests if the left range (the values from the table) is contained in the right hand range (the month you want to test). The comparison is done with an "open interval", which means '2019-09-01' is excluded from it. The above would not consider rows that do not contain the full August.
If you want to include partial matches as well, use the "overlaps" operator && instead:
select count(*)
from the_table
where daterange(start_date::date, end_date::date, '[]') && daterange('2019-08-01'::date, '2019-09-01'::date, '[)');
You can use to_date() in your where clause. So that your where clause would be like this.
WHERE '2019-08' BETWEEN to_date(start_date, 'YYYY-MM') and to_date(end_date ,'YYYY-MM')
I would recommend writing this as:
WHERE end_date >= TO_DATE('2019-08', 'YYYY-MM') AND
start_date < TO_DATE('2019-08', 'YYYY-MM') + INTERVAL '1 MONTH'
That is, the period includes at least one day of the month, because it starts before the end of the month and ends after the start of the month.
In addition, this has no functions on the columns in the table. So, if an index is available on either column, then it can be used. If you define the start/end as a range, then that provides other opportunities for using indexes.

Store day and date in same column in oracle

I have a situation where I need to store day and date(just a date) in single column in Oracle as below:
Column name
Monday
Tuesday
25
10
But I don't like combining string and number in single column. So I want any formula or design where each of these values gets unique identifier and store like below:
Column name
1 - - - - - refers to Monday
2............refers Tuesday
3-----------refers to a actual date 1
Above is an imaginary values just for understanding.
What I need is if have to get all entries with value Monday I should apply some formula and get a number which I should use in select query
Same way if I want to select entries with date =25 then I have to apply same formula and get unique number that represents date 25 and use it in my query
Store only the DATE in a column, the day of week is derived from that.
so:
CREATE TABLE junk
(
id number,
my_date date
);
insert into junk values ( 123, sysdate );
insert into junk values ( 234, add_months(sysdate,-123) );
commit;
select my_date "The actual date",
to_char(my_date,'Day') "The day of the week",
to_char(my_date,'Dy') "other way",
to_char(my_date,'D') "another way"
from junk;
The actual date The day o oth a
-------------------- --------- --- -
27-mar-2019 15:28:15 Wednesday Wed 4
27-dec-2008 15:28:15 Saturday Sat 7
2 rows selected.
Trying to do anything more than that, you will likely only hurt yourself in the long run (consider how you will maintain things if something is updated .. consider what happens if/when somebody "accidentally" updates 1 of the values, without updating the matching one? ... because they didn't understand it ... )
-- only pull records on Wednesday
select *
from junk
where to_char(my_date,'D') = 4
/
ID MY_DATE
---------- --------------------
123 27-mar-2019 15:28:15
1 row selected.
You can use the to_char() function in oracle:
SELECT TO_CHAR(date '2019-03-27', 'DAY') day FROM dual;
Which will return the day of the week.

SQL Query which returns set of tuples that exists for a given data range

I have a table say EmployeeAbsence that has three columns: EmployeeId, LeaveReason and Date. This table records attendance of employees on a daily basis. I want to know the list of employees who took leave in last 14 days.
For example,
1 XYZ 2009-07-20
2 XYZ 2009-07-19
--
---
--
1001 XYZ 2009-07-04
In this case, my query output should return XYZ and alike because it contains entries for employees who were absent for last 14 days consecutively. I would appreciate an ORACLE query.
Your query will need two constraints
Data of leave must be less than 14 days away from current date
Employee has to appear only once if they have been on leave for several days / times during the 14 day period
For constraint (1) you need to know that subtracting date A from date B results in the number of days between those two dates.
For constraint (2) you need to group by the employees ID.
That said
SELECT EmployeeID
FROM EmployeeAbsence
WHERE Date between SYSDATE - 14 and SYSDATE
GROUP BY EmployeeId
should do the trick.
I assume that table has 1 record for each day of absence and you don't want to retrieve employees that were absent for e.g. last month but returned during last 14 days.
SELECT employeeId
FROM employeeAbsences base
WHERE date > trunc(sysdate)-15 -- we want to include one more day for HAVING clause to work with
GROUP BY employeeId
-- had 2 or more non-consecutive leaves --> at least one of them started during last 14 days
HAVING count(*) < max(date) - min(date)
-- first absence is less than 14 days ago
OR min(date) > trunc(sysdate) - 14;