Do not input private or sensitive data. View Qlik Privacy & Cookie Policy.
Skip to main content

Announcements
Qlik GA: Multivariate Time Series in Qlik Predict: Get Details
cancel
Showing results for 
Search instead for 
Did you mean: 
Not applicable

SQL WHERE clause based on loaded data

Hi,

I'm not sure if this is possible in QlikView, but am hoping it is. I am loading a qvd file, then loading the latest data from SQL, finally storing the data back to the qvd file ready for the next day.

At the moment I load one day's data on each load. However if the QlikView file is not ran that day, the day is missing, and if it gets ran twice then the day is duplicated.

What I need is something like this:-

DataTable:



LOAD CalendarDate, FileSize
FROM C:\File.QVD (qvd);

CONCATENATE SQL SELECT CalendarDate, FileSize
FROM DW.dbo.vwFileSizes
WHERE calendardate > #$(max(CalendarDate))#;

STORE DataTable INTO C:\File.QVD;

The bit that does not work is the #$(max(CalendarDate))# bit. I want this to be the maximum date from the qvd load.

Any ideas?

1 Solution

Accepted Solutions
Not applicable
Author

Thanks John,

It's all working now. I've added the code below in case anyone else has the same issue. This also shows how to concatenate to an existing table other than the current table in use.

Thanks for all your help.

DataTable:

LOAD CalendarDate,

FileSize

FROM C:\File.QVD (qvd);

DataTable2:

load max(CalendarDate) as MDate resident DataTable;

let vMaxDate=peek('MDate');

concatenate (DataTable) SQL SELECT CalendarDate, FileSize

FROM DW.dbo.vwFileSizes

where calendardate > $(vMaxDate);

drop table DataTable2;

View solution in original post

6 Replies
Oleg_Troyansky
Partner Ambassador/MVP
Partner Ambassador/MVP

you are almost there. You need a couple of extra steps:

1. Load from QVD

2. Calculate max(Date) using resident load and group by

3. Use function peek() to assign the value to a variable vMaxDate

4. Use $-sign expansion to pass the value to SQL:

WHERE calendardate > $(vMaxDate)

cheers,

Ask me about Qlik Sense Expert Class!
johnw
Champion III
Champion III

I'll point out a potential pitfall - you might easily have a new file with the same date as a file already in the QVD. For example, you create a file, run the reload, create another file, and rerun the reload. You need to pick up the new file, not skip it just because it has the same date. To solve it, I would want to grab the max date records from the database instead of from the QVD. It's not as simple as just using ">=" of course, because that will double up on the max date records. How I would solve it depends on how much data you're expecting, though.

Not applicable
Author

Thanks for the reply Oleg. It looks better, but is still not right. Below is the code I now have.

When I run this I get an error, the vMaxDate passes through "peek(MDate,0,DataTable2)", rather than the actual date.

Once this works the current table is DataTable2, how do I concatenate the new data to DataTable?



DataTable:

LOAD

CalendarDate

,

FileSize

FROM

C:\File.QVD (qvd)

;

DataTable2:

load

max(CalendarDate) as MDate resident

DataTable;

set

vMaxDate=peek(MDate,0,DataTable2)

;

concatenate

SQL SELECT CalendarDate,

FileSize

FROM

DW.dbo.vwFileSizes

where



calendardate > $(vMaxDate)

;





johnw
Champion III
Champion III

I'm not certain, but I THINK you need to use LET instead of SET, and I think you need single quotes around MDate:

LET vMaxDate=peek('MDate');

rwunderlich
Partner Ambassador/MVP
Partner Ambassador/MVP

You can get a working example of a date based incremental load from the QV Cookbook at:

http://robwunderlich.com/Download.html

The specific example is titled "Incremental reload template and example."

-Rob

Not applicable
Author

Thanks John,

It's all working now. I've added the code below in case anyone else has the same issue. This also shows how to concatenate to an existing table other than the current table in use.

Thanks for all your help.

DataTable:

LOAD CalendarDate,

FileSize

FROM C:\File.QVD (qvd);

DataTable2:

load max(CalendarDate) as MDate resident DataTable;

let vMaxDate=peek('MDate');

concatenate (DataTable) SQL SELECT CalendarDate, FileSize

FROM DW.dbo.vwFileSizes

where calendardate > $(vMaxDate);

drop table DataTable2;