Qlik Set Analysis and aggregating across date ranges - qlikview

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!

Related

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

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?

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.

Max date among records and across tables - SQL Server

I tried max to provide in table format but it seem not good in StackOver, so attaching snapshot of the 2 tables. Apologize about the formatting.
SQL Server 2012
**MS Table**
**mId tdId name dueDate**
1 1 **forecastedDate** 1/1/2015
2 1 **hypercareDate** 11/30/2016
3 1 LOE 1 7/4/2016
4 1 LOE 2 7/4/2016
5 1 demo for yy test 10/15/2016
6 1 Implementation – testing 7/4/2016
7 1 Phased Rollout – final 7/4/2016
8 2 forecastedDate 1/7/2016
9 2 hypercareDate 11/12/2016
10 2 domain - Forte NULL
11 2 Fortis completion 1/1/2016
12 2 Certification NULL
13 2 Implementation 7/4/2016
-----------------------------------------------
**MSRevised**
**mId revisedDate**
1 1/5/2015
1 1/8/2015
3 3/25/2017
2 2/1/2016
2 12/30/2016
3 4/28/2016
4 4/28/2016
5 10/1/2016
6 7/28/2016
7 7/28/2016
8 4/28/2016
9 8/4/2016
9 5/28/2016
11 10/4/2016
11 10/5/2016
13 11/1/2016
----------------------------------------
The required output is
1. Will be passing the 'tId' number, for instance 1, lets call it tid (1)
2. Want to compare tId (1)'s all milestones (except hypercareDate) with tid(1)'s forecastedDate milestone
3. return if any of the milestone date (other than hypercareDate) is greater than the forecastedDate
The above 3 steps are simple, but I have to first compare the milestones date with its corresponding revised dates, if any, from the revised table, and pick the max date among all that needs to be compared with the forecastedDate
I managed to solve this. Posting the answer, hope it helps aomebody.
//Insert the result into temp table
INSERT INTO #mstab
SELECT [mId]
, [tId]
, [msDate]
FROM [dbo].[MS]
WHERE ([msName] NOT LIKE 'forecastedDate' AND [msName] NOT LIKE 'hypercareDate'))
// this scalar function will get max date between forecasted duedate and forecasted revised date
SELECT #maxForecastedDate = [dbo].[fnGetMaxDate] ( 'forecastedDate');
// this will get the max date from temp table and compare it with forecasatedDate/
SET #maxmilestoneDate = (SELECT MAX(maxDate)
FROM ( SELECT ms.msDueDate AS dueDate
, mr.msRevisedDate AS revDate
FROM #mstab as ms
LEFT JOIN [MSRev] as mr on ms.msId = mr.msId
) maxDate
UNPIVOT (maxDate FOR DateCols IN (dueDate, revDate))up );

MS ACCESS – Return a daily count of booked resources within a date range

Please note: this is not for an Access project as such, but a legacy application that uses an Access database for its back end.
Setup
Part of the application is a kind of Gantt chart, fixed to single day columns, where each row represents a single resource. Resources are booked out for a range of days and a booking is for a single resource, so they cannot overlap on a row. The range of dates that is in view is user selectable, open ended, and can be changed by various methods, including horizontal scrolling using mouse or keyboard.
Problem
I've been tasked with adding a row to the top of the chart to indicate overall resource usage for each day. Of course that's trivially easy to do by simply querying for each day in the range separately, but unfortunately that is proving to be an expensive process and therefore slows down horizontal scrolling a lot. So I'm looking for a way to do it more efficiently, hopefully with fewer database reads.
Here is a highly simplified example of the bookings table:
booking_ID | start_Date | end_Date | resource_ID
----------- -------------- ------------- -------------
1 2014-07-17 2014-07-20 21
2 2014-08-24 2014-08-29 4
3 2014-08-26 2014-09-02 21
4 2014-08-28 2014-09-04 19
Ideally, I would like a single query that returns each day within the specified range, along with a count of how many bookings there are on those days. So querying the data above for 20 days from 2014-07-17 would produce this:
check_Date | resources_Used
----------- ---------------
2014-07-17 1
2014-07-18 1
2014-07-19 1
2014-07-20 1
2014-07-21 0
2014-07-22 0
2014-07-23 0
2014-08-24 1
2014-08-25 1
2014-08-26 2
2014-08-27 2
2014-08-28 3
2014-08-29 3
2014-08-30 2
2014-08-31 2
2014-09-01 2
2014-09-02 2
2014-09-03 1
2014-09-04 1
2014-09-05 0
I can get a list of dates in the range by using a table of integers (starting at 0), with this:
SELECT CDATE('2014-07-17') + ID AS check_Date FROM Integers WHERE ID < 20
And I can get the count of resources used for a single day with something like this:
SELECT COUNT(*) AS resources_Used
FROM booking
WHERE start_Date <= CDATE('2014-09-04')
AND end_Date >= CDATE('2014-09-04')
But I can't figure out how (or if) I can tie them both together to get the desired results. Is this even possible?
Create a table called "calendar" and put a list of dates into it covering the necessary timeframe. It just needs one column called check_date with one row for each date. Use Excel, start at whatever date and just drag down, then import into the new table.
After your calendar table is set up you can run the following:
select c.check_date, count(b.resource_id) as resources_used
from calendar c, bookings b
where c.check_date between b.start_date and b.end_date
group by c.check_date