I would like to have one table in Qlikview with Year values stored in it. They should start from 2000 and end with the latest year (i.e. 2014).
I want to make it dynamic, so that next year, the values would be till 2015. How do I go about it?
P.S.
I tried following but it didn't work.
SET vInitYear = 2000;
YearSequence:
LOAD vInitYear AS YearSeq AutoGenerate(Year(Today));
Something like this should do the job...
FOR i = 0 to 15
LET NewDate = YEAR(UTC())-i;
LOAD * INLINE [
MyYear
$(NewDate)
];
NEXT i
FOR y = 2000 to Year(today())
Years:
LOAD $(y) as Year
Autogenerate 1;
NEXT y
Related
So I have a table with the following columns:
For each record in the above table (e.g., stock A with a ENTRY_DT as 2011.08.22 and REMOVE_DT as 2011.09.03), I’d like to replicate it for each day between the start and end date (excluding weekends). The converted records keep the same value of fields S_INFO_WINDCODE and SW_IND_CODE as the original record.
Table after conversion should look like this:
(only records of stock A are shown)
As the data volume is not large, you can process each record with cj(cross join), then use function unionAll to combine all records into the output table.
The table:
t = table(`A`B`C as S_INFO_WINDCODE, `6112010200`6112010200`6112010200 as SW_IND_CODE, 2011.08.22 1998.11.11 1999.05.27 as ENTRY_DT, 2011.09.03 2010.10.08 2011.09.30 as REMOVE_DT)
Solution:
def f(t, i) {
windCode = t[i][`S_INFO_WINDCODE]
code = t[i][`SW_IND_CODE]
entryDate = t[i][`ENTRY_DT]
removeDate = t[i][`REMOVE_DT]
days = entryDate..removeDate
days = days[weekday(days) between 1:5]
return cj(table(windCode as S_INFO_WINDCODE, code as SW_IND_CODE), table(days as DT))
}
unionAll(each(f{t}, 1..size(t) - 1), false)
Good day every one this is my trigger code that after the user will insert a data and select a pr date it will generate a control number in the database but my problem here is when the user pick a pr_date that is 01-28-2021 and after adding record it display a value in the datatabase 202103-1 it must display a value of 202101-1
BEGIN
SET #v1 = (SELECT control_number FROM tbl_worklog where DATE_FORMAT(pr_date,'%Y%m') =DATE_FORMAT(NOW(),'%Y%m') ORDER BY id DESC LIMIT 1);
IF (#v1 is null) THEN
SET new.control_number = (CONCAT(CAST(DATE_FORMAT(NOW(),'%Y%m') as CHAR),"-1"));
ELSE
SET #v2 = (CAST(SUBSTRING_INDEX(#v1,"-",-1) as int)+1);
SET new.control_number = (CONCAT(CAST(DATE_FORMAT(NOW(),'%Y%m') as CHAR),"-",CAST(#v2 as char)));
END IF;
END
To determine the control number, you're using the formula
DATE_FORMAT(NOW(), '%Y%m')
which will look at the current date, which is why you're getting 202103 (it's currently March, the third month). You should instead use
DATE_FORMAT(pr_date, '%Y%m')
I need to reproduce the kettle Datedif function in the R programming language. I need the 'datedif month' option. I thought reproducing would be pretty easy but I have some 'weird behaviour' in pentaho. As an example:
ID date_1 date_2 monthly_difference_kettle daydiff_mysql
15943 31/12/2013 28/07/2014 7 209
15943 31/12/2011 27/07/2012 6 209
So in pentaho kettle I used the formula-step and the function DATEDIF(date2,date1,"m"). As you can see when I calculate the daily difference in mysql I get for both records the same amount of days in difference (209), however, when the monthly difference is calculated via the formula step in pentaho kettle I get a different result in months (7 and 6 respectively). I don't understand how this is calculated...
Can anyone produce the source code for the 'DATEDIF months' function in pentaho? I would like to reproduce it in R so I get exactly the same results.
Thanks in advance,
best regards,
Not sure about mysql, but i think it is the same. In PostgreSQL date difference gives integer value (in days). It means both rows has total match in days.
Calculating month difference non trivial. What is month (28, 30, 31 day)? Shall we count if month is not full?
Documentation states If there is not a complete month between the dates, 0 will be returned
But according to source code easy to understand how calculated datedif:
Source code available via github https://github.com/pentaho/pentaho-reporting/blob/f7defbcfc0e8f48ad2b139fe9820445f052e0e78/libraries/libformula/src/main/java/org/pentaho/reporting/libraries/formula/function/datetime/DateDifFunction.java
private int addFieldLoop( final GregorianCalendar c, final GregorianCalendar target, final int field ) {
c.set( Calendar.MILLISECOND, 0 );
c.set( Calendar.SECOND, 0 );
c.set( Calendar.MINUTE, 0 );
c.set( Calendar.HOUR_OF_DAY, 0 );
target.set( Calendar.MILLISECOND, 0 );
target.set( Calendar.SECOND, 0 );
target.set( Calendar.MINUTE, 0 );
target.set( Calendar.HOUR_OF_DAY, 0 );
if ( c.getTimeInMillis() == target.getTimeInMillis() ) {
return 0;
}
int count = 0;
while ( true ) {
c.add( field, 1 );
if ( c.getTimeInMillis() > target.getTimeInMillis() ) {
return count;
}
count += 1;
}
}
Append 1 month to start date till it will become bigger then end date
My first question here..!
I'm not an expert in SQL, so bear over with me please! :)
I have a web page (not created by me) which gets report data from a MSSQL database, on the web page you enter start date and end date and data are fetched in this time interval from 00:00 on start date until 23:59 on end date.
I have managed to add more queries to the SQL, but now I would like to, for certain values only to return values which were logged in the time range 00:00:00 until 04:00:00 every day in the selected time interval.
Currently values are logged once an hour, but not always consistently. So far I have made a workaround in my web page which shows the first 4 values and skips the next 20, this loops for the selected interval. This method works 98% of the time, but occasionally there are more or fewer than 24 logged values per day which can cause the shown values will be skewed one way or another.
What I would like to do is change my SQL query so that it only returns values in the time range I want (between midnight and 04:00) for every day in the selected period. I hope someone can help me achieve this or give me some hints! :)
This is the existing SQL query running with the variables which I do want all values for. There are more variables than this but I edited them out, all the Ren*Time variables is the ones I want to make a 4-hour-every-day version of.
SET NOCOUNT ON;
IF OBJECT_ID('tempdb..#tmpValues') IS NOT NULL BEGIN DROP TABLE #tmpValues END;
CREATE TABLE #tmpValues(Id INT PRIMARY KEY IDENTITY(1,1),BatchId INT, TimePoint DATETIME, Ren1Time DECIMAL(10,2), Ren2Time DECIMAL(10,2), Ren3Time DECIMAL(10,2), RenTotTime DECIMAL(10,2));
INSERT INTO #tmpValues(BatchId)
SELECT BatchId
FROM Batch
WHERE Batch.LogTime BETWEEN <StartTime> AND <StopTime>;
CREATE UNIQUE INDEX I_BatcId ON #tmpValues(BatchId);
UPDATE #tmpValues SET
TimePoint = (SELECT LogTime FROM Batch WHERE Batch.BatchId = #tmpValues.BatchId),
Ren1Time = (SELECT SUM(_Float) FROM LogData WHERE LogData.BatchId = #tmpValues.BatchId AND LogData.TagId = 21),
Ren2Time = (SELECT SUM(_Float) FROM LogData WHERE LogData.BatchId = #tmpValues.BatchId AND LogData.TagId = 25),
Ren3Time = (SELECT SUM(_Float) FROM LogData WHERE LogData.BatchId = #tmpValues.BatchId AND LogData.TagId = 29),
RenTotTime = (SELECT SUM(_Float) FROM LogData WHERE LogData.BatchId = #tmpValues.BatchId AND (LogData.TagId = 25 OR LogData.TagId = 29 OR LogData.TagId = 33));
DECLARE
#TimePoint DATETIME,
#Ren1Time FLOAT,
#Ren2Time FLOAT,
#Ren3Time FLOAT,
#RenTotTime FLOAT;
INSERT INTO #tmpValues(TimePoint, Ren1Time, Ren2Time, Ren3Time, RenTotTime)
VALUES(#TimePoint, #Ren1Time, #Ren2Time,#Ren3Time, #RenTotTime);
SET NOCOUNT OFF;
SELECT * FROM #tmpValues;
IF OBJECT_ID('tempdb..#tmpValues') IS NOT NULL BEGIN DROP TABLE #tmpValues END;
Don't mess around with temp tables and processing every column separately. I also have no idea what you're trying to do with those variables. You declare them, never set them, then do an INSERT with them, which will just insert a row of NULL values.
Assuming that you're using SQL Server, the DATEPART function will let you get the hour of the day.
SELECT
B.BatchID,
B.LogTime AS TimePoint,
SUM(CASE WHEN B.TagId = 21 THEN _Float ELSE 0 END) AS Ren1Time,
SUM(CASE WHEN B.TagId = 25 THEN _Float ELSE 0 END) AS Ren2Time,
SUM(CASE WHEN B.TagId = 29 THEN _Float ELSE 0 END) AS Ren3Time,
SUM(CASE WHEN B.TagId IN (21, 25, 29) THEN _Float ELSE 0 END) AS RenTotTime
FROM
dbo.Batch B
INNER JOIN LogData LD ON LD.BatchId = B.BatchId
WHERE
B.LogTime BETWEEN <StartTime> AND <StopTime> AND
DATEPART(HOUR, B.LogTime) BETWEEN 0 AND 4
GROUP BY
B.BatchID,
B.TimePoint
Thank you very much for your swift reply!
I couldn't exactly get your suggestion to work, but I did solve my problem thanks to your suggestion!
Since the SQL query which should be limited to the 4 hours is used only from one webpage and there are no values in this web page which should be shown for all 24 hours, I thought I could copy the existing .SQL file to a new file and simply change the following:
WHERE Batch.LogTime BETWEEN <StartTime> AND <StopTime>;
To
WHERE Batch.LogTime BETWEEN <StartTime> AND <StopTime> AND DATEPART(HOUR, Batch.LogTime) BETWEEN 0 AND 3;
I changed the call to the .SQL file in the web page from the old to the new file and it works! Simple as that! Also changed 0 AND 4 to 0 AND 3 since I found 03:59:59 will be included, which is exactly what I want :)
just for documentation and future readers, the problem was solved and the answer is in the discussion between me and #varocarbas
My Application is reading a few columns from access data base and joining those columns into an existing datatable. one of the columns in the database has dates, the column is configured as date and time column in the database and in the data column of the datatable. the problem is when i am extrcting it into excel ,some of the dates are behaving in the oposite direction (i think the term is "english date"). what that i mean by "behaving", is that excel recognize the monthes as the days and the days as the months, for example a date that should be 11 in september 2015 the excel formula month(11/09/2015) is showing that the month is november and not september.
also, the date values in the access column are from sharepoint list that is also configured as dateandtime.. i have a filling that this behaving maybe belongs to excel(?),
here is the importent parts of my code:
' declaring the dateandtime column
Dim colString As DataColumn = New DataColumn("changed")
colString.DataType = System.Type.GetType("System.DateTime")
UnitedTable.Columns.Add(colString)
Storing it from the database:
If Not (IsDBNull(UnitedTable.Rows(i).Item("spcall"))) Then
spcall = UnitedTable.Rows(i).Item("spacll")
rowNum = Searchbinary(spcall) ' check that spcall exist in Projects table via binary search
If rowNum > -1 Then
UnitedTable.Rows(i).Item("somecolumn") = db.ReadDataSet.Tables(0).Rows(rowNum).Item("PmUserName").ToString
UnitedTable.Rows(i).Item("somecolumn") = db.ReadDataSet.Tables(0).Rows(rowNum).Item("OpertionalStatus").ToString
UnitedTable.Rows(i).Item("somecolumn") = db.ReadDataSet.Tables(0).Rows(rowNum).Item("CrmCallNum").ToString
UnitedTable.Rows(i).Item("somecolumn") = db.ReadDataSet.Tables(0).Rows(rowNum).Item("OperationStart").ToString
UnitedTable.Rows(i).Item("somecolumn") = db.ReadDataSet.Tables(0).Rows(rowNum).Item("Approved").ToString
UnitedTable.Rows(i).Item("somecolumn") = db.ReadDataSet.Tables(0).Rows(rowNum).Item("GeneralStatus").ToString
UnitedTable.Rows(i).Item("changed") = db.ReadDataSet.Tables(0).Rows(rowNum).Item("Changed")
Else
' the spcall not exist add it to arraylist
SpCalls_NotExist.Add(spcall)
'UnitedTable.Rows(i).Delete()
End If
Then I am populating it to excel with nested loop.