The obvious method is to use macros to retrieve the data using ADO calls.
However, there is a function in QlikView that can do this - but only to retrieve one value (e.g. a record count). The function is called SqlValue.
The function takes 3 parameters:
- Connection Type
Connection Type is only one of 'OLEDB' or 'ODBC'. If you leave it out, ODBC is assumed.
The Query is a string that should (although not absolutely required) return one row and one value. If it returns more, only the first value in the first row is returned.
The Connection is straightforward if using ODBC because you just use the predefined ODBC connection name. For OLEDB, you need to pass an OLEDB connection string. Now, this can be messy (as they can get quite long and include single quotes which can throw you a bit. For me, the easiest way is to set up a variable with the Connection string in it (perhaps it could be stored in the registry and retrieved using GetRegistryString!)
Once you have it, then you just call like this:
=Num(SqlValue(MyDatasource, 'SELECT Distinct Count(*) As MyCount From Orders', 'OLEDB'))
Note that I have used the Num function here to cast the return value as a numeric. The SqlValue function returns a dual so, if you want to display it, you need to cast as numeric or it will display as blank (no Text value in the Dual).
The query that you pass can be as complex as the target database supports and can use values from your QlikView document:
=Num(SqlValue(MyDatasource, 'SELECT Distinct Count(*) As MyCount From Orders Where CustomerID in (' & Concat(Distinct CustomerID, ',') & ')', 'OLEDB'))
Obviously, and this is flagged in the help file, if you have queries running in your document, they can slow things down while waiting for the database query to return.