Skip to main content
Woohoo! Qlik Community has won “Best in Class Community” in the 2024 Khoros Kudos awards!
Announcements
Nov. 20th, Qlik Insider - Lakehouses: Driving the Future of Data & AI - PICK A SESSION
cancel
Showing results for 
Search instead for 
Did you mean: 
satya_s
Partner - Contributor III
Partner - Contributor III

How to pass multiple values from filter to a SQL query in data load script?

Hi,

I have the following requirement:

1.User selects Year (single value) and Month (multiple values) from the filters in a sheet

2.User clicks on Reload button (Button Chart Type), which then passes the values to the Data Load scripts

3.The SQL query in Data Load script has 'Year' and 'Month' in the WHERE condition and single/multiple values needs to be passed. I guess it would be something like this. 

SELECT COUNT(OrderID) NoOfOrders, SUM(OrderAmount) OrderAmount
FROM Orders
WHERE TO_CHAR(OrderDate,'YYYY') = '$(vYearID)'
AND TO_CHAR(OrderDate,'MM') IN '$(vMonthID)'

Would the IN condition work in the SQL query, if the vMonthID is populated as 9,10,11?

Can someone please share the steps or a sample file for reference?

Many Thanks !!

 

 

 

Labels (1)
1 Solution

Accepted Solutions
satya_s
Partner - Contributor III
Partner - Contributor III
Author

Hi @anat 

Thanks for your reply. When I am selecting the filter, the multiple values are already comings with a comma delimiter. The issue was with the SQL formation.

The curly brackets after the IN statement needs to be explicitly defined for the SQL to work. I changed the SQL query to the one below and then it worked.

SELECT COUNT(OrderID) NoOfOrders, SUM(OrderAmount) OrderAmount
FROM Orders
WHERE TO_CHAR(OrderDate,'YYYY') = $(vYear)
AND TO_CHAR(OrderDate,'MM') IN ( $(vMonth) )

Thanks once again for looking into it.

View solution in original post

6 Replies
satya_s
Partner - Contributor III
Partner - Contributor III
Author

Hi,

Please note that I am able to refresh the data in the SQL when there is only 1 value in the variables vYear and vMonth variables. The following works...

vYear = GetFieldSelections(YearID);
vMonth = GetFieldSelections(MonthID);
The above variables are set in the Reload button configuration under 'Actions and Navigation'.

satya_s_0-1703876798485.png

SELECT COUNT(OrderID) NoOfOrders, SUM(OrderAmount) OrderAmount
FROM Orders
WHERE TO_CHAR(OrderDate,'YYYY') = $(vYear)
AND TO_CHAR(OrderDate,'MM') = $(vMonth)

The issue comes when there are multiple values selected from the filter. For example if I select Months 9,10 and 11 from the filter then the SQL query fails on click of Reload button because the IN condition doesn't work.

SELECT COUNT(OrderID) NoOfOrders, SUM(OrderAmount) OrderAmount
FROM Orders
WHERE TO_CHAR(OrderDate,'YYYY') = $(vYear)
AND TO_CHAR(OrderDate,'MM') IN $(vMonth)

How can I make the SQL to work with a IN condition when the variable vMonth is having multiple values? 

Kindly help.

Bjorn_Wedbratt
Former Employee
Former Employee

Hi @satya_s 

Try using the CONCAT() function to get all values in the month field into a comma separated list and pass it in the IN statement. See if that works

 

vMonth = concat(GetFieldSelections(MonthID),',')

Best

Björn 

anat
Master
Master

use concat function to store multiple values in variable

Concat(DISTINCT GetFieldSelections(Month),', ')

 

https://help.qlik.com/en-US/sense/November2023/Subsystems/Hub/Content/Sense_Hub/ChartFunctions/Strin....

 

marcus_sommer

It depends mainly on the from the data-base supported syntax. I suggest you comment the month-variable at first and writes two month manually within the in() or in "...." or whatever the data-base recognized. If it worked you shows the variable-content within a text-box until it looked like your hard-coded values.

satya_s
Partner - Contributor III
Partner - Contributor III
Author

Hi @anat 

Thanks for your reply. When I am selecting the filter, the multiple values are already comings with a comma delimiter. The issue was with the SQL formation.

The curly brackets after the IN statement needs to be explicitly defined for the SQL to work. I changed the SQL query to the one below and then it worked.

SELECT COUNT(OrderID) NoOfOrders, SUM(OrderAmount) OrderAmount
FROM Orders
WHERE TO_CHAR(OrderDate,'YYYY') = $(vYear)
AND TO_CHAR(OrderDate,'MM') IN ( $(vMonth) )

Thanks once again for looking into it.

satya_s
Partner - Contributor III
Partner - Contributor III
Author

Hi Marcus,

Thanks for your reply. I managed to make it work by explicitly putting the curly brackets in the SQL. 

SELECT COUNT(OrderID) NoOfOrders, SUM(OrderAmount) OrderAmount
FROM Orders
WHERE TO_CHAR(OrderDate,'YYYY') = $(vYear)
AND TO_CHAR(OrderDate,'MM') IN ( $(vMonth) )

Thanks once again for looking into it.