Creating a time dependent table in SQL for master data (system-versioned temporal tables) - sql

For a datawarehouse environment in SQL Server I have a master data sheet. However, I want to get to see all the changes over time.
Example of the current dataset:
id
Department
number of employees
datefrom
dateto
1
sales
14
2023-01-12
9999-12-31
2
finance
2
2023-01-12
9999-12-31
The data set we get is like:
id
Department
number of employees
modifying date
1
sales
14
2023-01-12
2
finance
2
2023-01-12
In this case, the modifying date is the date from of the current data set. Now the data set has been updated and the data set looks like:
id
Department
number of employees
modifying date
1
sales
18
2023-01-14
2
finance
9
2023-01-19
Now I want to update the current data set so it look like the following data set:
id
Department
number of employees
datefrom
dateto
1
sales
14
2023-01-12
2023-01-13
1
sales
18
2023-01-14
9999-12-31
2
finance
2
2023-01-12
2023-01-18
2
finance
9
2023-01-19
9999-12-31
Is this possible with system-versioned temporal tables from SQL or do I need to write a stored procedure?

Related

calculate monthly report [duplicate]

This question already has answers here:
How can I use SUM for bit columns?
(8 answers)
How can I get a count of a bit-type column?
(3 answers)
Closed last month.
I am trying to display reports in which based on employee code, I have to display how many days he/she presents on monthly basis.
Emp_Code Date Attendance
1001 2023-01-01 1
1001 2023-01-02 1
1001 2023-01-03 1
1001 2023-01-04 1
1001 2023-01-05 0
1001 2023-01-06 0
1001 2023-01-07 1
1001 2023-01-08 1
.
.
.
1002 2023-01-01 1
1002 2023-01-02 0
1002 2023-01-03 1
.
.
.
1003 2023-01-01 1
Attendance is a boolean column (0,1) 0 - This means the employee is absent, and 1 Means the employee is present on that day.
SQL query to Calculate monthly report as follow:
1001 Jan 2
1001 Feb 1
1001 Mar 0
1001 Apr 2
I wrote the query but shows me the number of present days, can anyone help me to correct it plz?
SELECT Emp_Code,MAX(DATENAME(MM,Date)) AS MonthWisePresentOrAbsent, COUNT(1) AS "DaysAbsent"
FROM tblEmployee GROUP BY MONTH(Date),Emp_Code;
Edit Question 1
it is a simple concept like every day in January month, 1 to 25 days if I am present then the last column contains the 1 and for 26 to 31 it contains 0.

SQL Query - Identifying entries between payment dates greater than 6 years

I have this table (in reality it has more fields but for simplicity, it will demonstrate what I'm after)
Payment_Type
Person ID
Payment_date
Payment_Amount
Normal
1
2015-01-01
£1.00
Normal
1
2017-01-01
£2.00
Reversal
1
2022-01-09
£3.00
Normal
2
2016-12-29
£3.00
Reversal
2
2022-01-02
£4.00
I need 2 specific things from this:
I need all entries where there is over 6 years difference between any given payment dates (when its been greater than or equal to 6 years from the date of the latest payment date). I don't need to count them, I just need it to return all the entries that meet this criteria.
I also need it to specify where a normal payment hasn't been made for 6 years or more from todays date but a reversal has however occurred within the last 6 years. (This might need to be a separate query but will take suggestions)
I'm using Data Lake (Hue).
Thank you.
I've tried to run a sub query with join and union but I'm not getting the desired results so will need to start from scratch. Any advice/insight on this is greatly appreciated.
Ideally, query one will show:
Payment_Type
Person ID
Payment_date
Payment_Amount
Normal
1
2015-01-01
£1.00
Normal
1
2017-01-01
£2.00
Normal
2
2016-12-29
£3.00
Query 2 results should show:
Payment_Type
Person ID
Payment_date
Payment_Amount
Normal
1
2017-01-01
£2.00
Reversal
1
2022-01-09
£3.00
Normal
2
2016-12-29
£3.00
Reversal
2
2022-01-02
£4.00

Populate Min/Max based on Issues and lead time

I am looking to write an sql update query to populate the min/max fields based on the issues and the date issued. I am looking at 120 days delivery as my delivery date.
PN
QTY_ISSUED
ISSUED_DATE
12345
2
20-01-01
12345
1
20-02-01
12345
2
20-03-01
12345
5
20-04-01
12345
1
20-04-20
12345
3
20-06-01
I would like to return and update to 11 and not return the 3 in June.

Qlik Set Analysis and aggregating across date ranges

I am trying to use Qlik set analysis and range sum to compute the total number of records that were active at a some point in the month
For example, if these are the records:
id state created_date modified_date
1 expired 01/12/2014 10/12/2014
2 expired 01/12/2014 10/03/2015
3 active 01/12/2014 01/12/2014
4 expired 10/01/2015 12/01/2015
5 expired 10/01/2015 11/03/2015
6 active 10/02/2015 10/02/2015
7 expired 10/03/2015 11/03/2015
The expected o/p is
Dec-14 3
Jan-15 4
Feb-15 4
Mar-15 5
E.g: For Jan-2015 the result should be 4 -
1 active record from Dec 2014 + 2 created in Jan + 1 from Dec which actually expired in March 2015
modified_date is updated when a record is expired
So I tried this:
rangesum(above(Count({$<[state] = {'active'} >} id), 0,12))
+ Count({1<[state] ={'expired'}, modified_date.Calendar.Month ={">=$(created_date.Calendar.Month)"}>} id)
the second count statement is wrong. How can I aggregate the records which are currently not active but were active at some point during the month.Is it possible to achieve this without using a Master calendar ?
Thanks in advance!
A better approach to this would be to amend your data model to include an active date like below.
ActiveMonthsRaw:
Load ID
,date(monthstart(created_date),''MMM-YY) as ActiveMonthFormat
resident SourceTable
where state ='active';
ActiveMonthsRaw:
Load ID
,date(monthstart(modified_date),''MMM-YY) as ActiveMonthFormat
resident SourceTable
where state ='expired';
ActiveMonths:
Load distinct ID
,ActiveMonthFormat as ActiveMonth;
drop table ActiveMonthsRaw;
It would then be a case of doing a distinct count of ID by the Active Month. No Set analysis in sight!

Subtract nonconsecutive values in same row in t-SQL

I have a data table that has annual data points and quarterly data points. I want to subtract the quarterly data points from the corresponding prior annual entry, e.g. Annual 2014 - Q3 2014, using t-SQL. I have an id variable for each entry, plus a reconcile id variable that shows which quarterly entry corresponds to which annual entry. See below:
CurrentDate PreviousDate Value Entry Id Reconcile Id Annual/Quarterly
9/30/2012 9/30/2011 112 2 3 Annual
9/30/2013 9/30/2012 123 1 2 Annual
9/30/2014 9/30/2013 123.5 9 1 Annual
12/31/2013 9/30/2014 124 4 1 Quarterly
3/31/2014 12/31/2013 124.5 5 1 Quarterly
6/30/2014 3/31/2014 125 6 1 Quarterly
9/30/2014 6/30/2014 125.5 7 1 Quarterly
12/31/2014 9/30/2014 126 10 9 Quarterly
3/31/2015 12/31/2014 126.5 11 9 Quarterly
6/30/2015 3/31/2015 127 12 9 Quarterly
For example, Reconcile ID 9 for the quarterly entries corresponds to Entry ID 9, which is an annual entry.
I have code to just subtract the prior entry from the current entry, but I cannot figure out how to subtract quarterly entries from annual entries where the Entry ID and Reconcile ID are the same.
Here is the code I am using, which is resulting in the right calculation, but increasing the number of results by many rows. I have also tried this as an inner join. I only want the original 10 rows, plus a new difference column:
SELECT DISTINCT T1.[EntryID]
, [T1].[RECONCILEID]
, [T1].[CurrentDate]
, [T1].[Annual_Quarterly]
, [T1].[Value]
, [T1].[Value]-T2.[Value] AS Difference
FROM Table T1
LEFT JOIN Table T2 ON T2.EntryID = T1.RECONCILEID;
Your code should be fine, here's the results I'm getting:
EntryId Annual_Quarterly CurrentDate ReconcileId Value recVal diff
2 Annual 9/30/2012 3 112
1 Annual 9/30/2013 2 123 112 11
9 Annual 9/30/2014 1 123.5 123 0.5
4 Quarterly 12/31/2013 1 124 123 1
5 Quarterly 3/31/2014 1 124.5 123 1.5
6 Quarterly 6/30/2014 1 125 123 2
7 Quarterly 9/30/2014 1 125.5 123 2.5
10 Quarterly 12/31/2014 9 126 123.5 2.5
11 Quarterly 3/31/2015 9 126.5 123.5 3
12 Quarterly 6/30/2015 9 127 123.5 3.5
with your data and this SQL:
SELECT
tr.EntryId,
tr.Annual_Quarterly,
tr.CurrentDate,
tr.ReconcileId,
tr.Value,
te.Value AS recVal,
tr.[VALUE]-te.[VALUE] AS diff
FROM
t AS tr LEFT JOIN
t AS te ON
tr.ReconcileId = te.EntryId
ORDER BY
tr.Annual_Quarterly,
tr.CurrentDate;
Your question is a bit vague as far as how you're wanting to subtract these values, but this should give you some idea.
Select T1.*, T1.Value - Coalesce(T2.Value, 0) As Difference
From Table T1
Left Join Table T2 On T2.[Entry Id] = T1.[Reconcile Id]