Retrieve field from table B based on date in table A - sql

I have table A which contains tasks and completion dates and table B that contains the Fiscal Year, and production cycles. This is the structure of table B:
FY
Cycle
StartDate
EndDate
2021
2
2021-03-31
2021-06-30
2021
3
2021-07-01
2021-31-12
2022
1
2022-01-01
2022-03-31
2022
2
2022-03-31
2022-06-30
What I want to do is to retrieve Cycle based on whether my date in Table A falls between a StartDate and EndDate.
My resultset would be my date and then the cycle. Eg.
Task with date 2022-02-22 08:43:44.002000 falls between 2022-01-01 and 2022-03-31 so I want to retrieve cycle 1 in FY 2022.
Since I cannot join this table directly and check it with a BETWEEN I tried to create a CTE containing the entire table of B but then I kind of got stuck with the next steps. There must be a better approach than the code below (which doesn't work).
WITH cte AS (
SELECT * FROM B
)
CASE WHEN task_date >= (SELECT StartDate FROM cte) AND task_date < (SELECT EndDate FROM CTE)

Could i perhaps point you to a question i've asked in the past where i was trying to link data from 2 tables in separate databases where there was no link between them. The answer from Squirrel was able to give me exactly what i needed:
How To Insert Data Into Temp Table From Different Databases
It basically involves adding a Row Number to each table and joining on that Row Number. You might be able to use that logic to get what you need.

Related

Date format in sas concatenate with a proc sql under conditions

i have a macro_variable :%let date=201909
and table :
ID Sending-date item
1 15-jul-2019 A
2 23-sep-2019 B
3 12-sep-2019 A
4 1-jan-2019 B
5 5-feb-2019 B
What i'm wondering to do is to verify if there is an item sent in the month indicated in my date (09 september) and the two previous months (august and july) using a proc sql and without adding new variables.
the result_table expected is like this :
Month Year Number of items
9 2019 2
8 2019 0
7 2019 1
The biggest problem is how to convert the format of the date in the table like my macro_variable date.
Here's one method.
I used cutoff_date instead of date, because it helps differentiate the dates more easily.
Use INTNX() to do date calculations. In this case, I set the cutoff to be the end of cutoff_month and the start of two months prior. You may need to define that a bit more clearly to meet your needs but this works.
%let cutoff_date=201909;
proc sql;
create table want as
select month(sending_date) as Month, count(*) as num
from have
where sending_date between intnx('month', input("&cutoff_date.", yymmn6.), 0, 'e') and intnx('month', input("&cutoff_date.", yymmn6.), -2, 'b')
group by calculated Month;
quit;

How to make a (MDX) calculation in a SSAS cube to get the difference between a value from the current row and a value from the previous row?

I would like to make a calculation to get the difference between the departDate from my current row and the arriveDateNextStop from my previous row. I have a fact table which has multiple columns. The three most important columns are: id, departDate, arriveDateNextStop.
If I have for example these two rows in my fact table:
id departDate arriveDateNextStop
1 01-01-2019 03-01-2019
1 04-01-2019 07-01-2019
Explanation: On 1 January 2019 I depart to the next destination and I arrive there on 3 January 2019. On 4 January 2019 I again depart to the next destination and I arrive there on 7 January 2019.
Now I would like to know how many days the idle time was (the amount of days between the arrival and the next depart). So with this example the idle time would be 1, because between 3 January 2019 and 4 January 2019 is one day.
First, I made this 'calculation' in Management Studio as a SQL query. See query below:
SELECT s.Id, s.departDate as Depart_current_place, s.arriveDateNextStop as Arrival_next_stop, LAG(arriveDateNextStop) OVER (ORDER BY arriveDateNextStop) AS Arrival_current_stop, DATEDIFF(DAY, LAG(arriveDateNextStop) OVER (ORDER BY arriveDateNextStop), departDate) AS Amount_of_days
FROM MyTable s
WHERE Id = 9
GROUP BY s.departDate, s.Id, s.arriveDateNextStop
ORDER BY s.departDate
This query works fine, but how can I do this in my cube as a calculation in MDX?
I don't have the same example, but the similar cube structure with Completed/Received date:
with
member departDate as [Received].[Year Qtr Month].CurrentMember.Member_Key
member arriveDate as [Completed].[Year Qtr Month].CurrentMember.Member_Key
member arriveDateNextStop as [Completed].[Year Qtr Month].CurrentMember.Lead(1).Member_Key
member idleDays as departDate-arriveDateNextStop
SELECT NON EMPTY { departDate,arriveDate,arriveDateNextStop,idleDays } ON 0
, NON EMPTY
{ ([Completed].[Year Qtr Month].[Date].ALLMEMBERS
* [Received].[Year Qtr Month].[Date].ALLMEMBERS ) } ON 1
FROM ( SELECT ( { [Completed].[Year Qtr Month].[Date].&[6213] } ) ON COLUMNS
FROM [MyCube])
I also have integer key for a date dimension (CurrentMember.Member_Key). 1 = 1998-01-01, 2 = 1998-01-02 etc. till today. You need to create a property in a Date dimension if your Date key is classic YYYYMMDD (which you cannot subtract to get days difference, I can do that in my example). And use it like CurrentMember.Properties("property name") instead of Member_Key.
Main formula part: Lag/Lead function to get prev. or next member.
Please update in case of questions.

how to insert data into table based on dates and frequency in MS Access 2007

I'm new to MS Access and my question has two parts, I'm working on Database to schedule jobs based on [StartDate], [EndDate] and [Frequency(Days)] of task, for example if the startdate is 01/01/2015 and the enddate is 31/12/2015 and Frequency is 30 days, then the query should add 12 dates like 31/01/2015, 01/03/2015, 31/03/2015....26/12/2015.
The second part of my question, if after two months I have changed the date like instead of 31/03/2015 the date has been changed manually to 05/04/2015 then the data should automatically changed from 05/04/2015 until 31/12/2015 without changing the completed dates from 01/01/2015 to 05/04/2015.

Earliest and Lastdate for each year in sql

I have a column with 3 columns. I have multiple records for a year. As you see some of my records as follows
ID stardate enddate
1 1/1/2010 5/3/2010
2 2/4/2010 NULL -**EDIT**
3 1/2/2011 5/6/2011
4 3/4/2011 NULL -**EDIT**
I want to get a result for the earliest date in that year and the last date in that year. So output could be like
**EDITED:** 1/1/2010 12/31/2010 - For Year 2010
**EDITED:** 1/2/2011 12/31/2011 - For Year 2011
How can i get that in a query?If you need more info,please ask. Thanks
EDIT: If for the year if one of the columns read NULL then I have to consider the last day of the year as the enddate. i.e.12/31/YYYY. And I need to do that for each year again.
Assuming you use DATE (or related) columns in a MySQL table, something like this should serve your request:
SELECT MIN(startdate),
MAX(enddate),
YEAR(startdate)
FROM my_table
GROUP BY YEAR(startdate);
This groups all entries by year (of the startdate) and show you the minimum and maximum entries for each year as you want. See also the documentation for the DATE function in MySQL.
There are similar date functions and possibilities if you are using an other database system. Usually you can easily find them by googling the database system and something like "date functions".
select MIN(stardate),max(enddate)
from [Tablename]
where YEAR(enddate)=2013

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;