Search code examples
stored-proceduresqlikviewqliksense

Iteration from an existing table to create another table - Qlikview


I apologize if the title is not clear enough but is this possible to do in Qlikview?

This is the original table loaded from database.

Variable   Status   Date                    Duration (Hours)
A          StatusA  9/10/2017 18:30:00.00   4
A          StatusB  9/10/2017 23:30:00.00   5
B          StatusA  9/10/2017 10:00:00.00   8
B          StatusB  9/10/2017 21:45:00.00   9

And how I want this to process.

Variable   Status   Date                    Duration (Hours)    FinishDate
A          StatusA  9/10/2017 18:30:00.00   4                   9/10/2017 22:30:00.00
A          StatusB  9/10/2017 23:30:00.00   0.5                 9/10/2017 23:59:59.59
A          StatusB  9/11/2017 0:00:00.00    4.5                 9/11/2017 3:30:00.00
B          StatusA  9/10/2017 10:00:00.00   8                   9/10/2017 18:00:00.00
B          StatusB  9/10/2017 21:45:00.00   2.25                9/10/2017 23:59:59.59
B          StatusB  9/11/2017 0:00:00.00    6.75                9/11/2017 6:15:00.00

I know this is possible through resident table but how to check if the Variable's running time exceeded a day then creates another row to separate the duration to next day. This is the case because I have a filter by week so if the last day (or Saturday) has a Variable with exceeded duration, data won't be accurate.

The result data will need to get the total duration per Variable which is filtered by week.

Script:

T1:
LOAD *, timestamp(Timestamp+[Duration Hours]/24) as FinishDate;
LOAD *, timestamp(Timestamp#(left(Date,19),'DD-MM-YYYY hh:mm:ss')) as Timestamp;
LOAD
    [EquipmentID] AS [Equipment ID],
    [TransactionDate] AS [Date],
    [LotID] AS [Lot ID],
    [Status] AS [Status],
    [DurationHours] AS [Duration Hours];
SQL 
    SELECT *
    FROM [SQL_SourceDB].[dbo].[SourceTable]
    WHERE [TransactionDate] >= '2016-01-01 00:00:00.000' AND [TransactionDate] <= '2016-01-31 00:00:00.000';

Left Join // add a split-flag where needed
LOAD Distinct Timestamp,FinishDate,fabs(Date(left(FinishDate,10))-Date(left(Timestamp,10)) >=1) as SplitFlag
Resident T1;

T2: // load first part (current day) of split-flag=1
LOAD 
    [Equipment ID],
    [Timestamp] AS [Date],
    [Lot ID],
    [Status],
    round((DayEnd(Timestamp)-Timestamp)*24,0.1) AS [Duration Hours]
Resident T1
Where SplitFlag=1;
Concatenate // load second part (next day) where split-flag=1
LOAD 
    [Equipment ID],
    daystart(FinishDate) AS [Date],
    [Lot ID],
    [Status],
    round((FinishDate-daystart(FinishDate))*24,0.1) AS [Duration Hours]
Resident T1
Where SplitFlag=1;
Concatenate // add the rest of the data (split-flag=0)
LOAD * Resident T1 Where SplitFlag=0;

DROP Table T1;   

Solution

  • This should work (see comments in code):

    T1: // first need to set a proper timestamp and FinishDate
    Load *, timestamp(Timestamp+[Duration (Hours)]/24) as FinishDate;
    Load *, timestamp(Timestamp#(left(Date,19),'DD-MM-YYYY hh:mm:ss')) as Timestamp;
    LOAD * INLINE [
        Variable, Status, Date, Duration (Hours)
        A, StatusA, 10-09-2017 18:30:00.00, 4
        A, StatusB, 10-09-2017 23:30:00.00, 5
        B, StatusA, 10-09-2017 10:00:00.00, 8
        B, StatusB, 10-09-2017 21:45:00.00, 9
    ];
    
    left join // add a split-flag where needed
    Load Distinct Timestamp,FinishDate,fabs(Date(left(FinishDate,10))-Date(left(Timestamp,10)) >=1) as SplitFlag
    Resident T1;
    
    T2: // load first part (current day) of split-flag=1
    Load Variable,Status,
    	 Timestamp,
    	 dayEnd(Timestamp) as FinishDate,
    	 round((DayEnd(Timestamp)-Timestamp)*24,0.1) as [Duration (Hours)]
    Resident T1
    where SplitFlag=1;
    Concatenate // load second part (next day) where split-flag=1
    Load Variable,Status,
    	 daystart(FinishDate) as Timestamp,
    	 FinishDate,
    	 round((FinishDate-daystart(FinishDate))*24,0.1) as [Duration (Hours)]
    Resident T1
    where SplitFlag=1;
    Concatenate // add the rest of the data (split-flag=0)
    Load * Resident T1 Where SplitFlag=0;
    
    drop table T1;