• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

queryFilter

New Here ,
Mar 10, 2022 Mar 10, 2022

Copy link to clipboard

Copied

How to code 'queryFilter'. I followed the instuctions CF user guide to the letter (and all other variations) and I'm still stuck.

Views

210

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 10, 2022 Mar 10, 2022

Copy link to clipboard

Copied

Did you look at the examples? Did you try running the first one in CFFiddle? This is kind of a complicated function for me to explain, so it might be better just to see a small example working in CFFiddle.

 

That said, the way this function works is a lot like JavaScript functions work nowadays. The second argument of the function often points to another unnamed function, like this:

 

filteredQuery = QueryFilter(myQuery, function(obj) {
     return obj.amount >30
})
 
In the above example, obj is a value that changes to represent each individual row from the query. Each row has a column called "amount", so we're looking at the amount of each row as QueryFilter iterates through the set of rows. If amount is greater than or equal to 30, that row is stored in the new, filtered query. So given a set of amounts that looks like 10, 20, 30, 40, 50 the QueryFilter function will identify the third, fourth and fifth rows and copy them into a new query object called "filteredQuery". The function is called QueryFilter because you always get a subset of your original query.

 

Dave Watts, Eidolon LLC

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Mar 10, 2022 Mar 10, 2022

Copy link to clipboard

Copied

 @Alan4a, show us your test code and we'll show you where you're going wrong.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 10, 2022 Mar 10, 2022

Copy link to clipboard

Copied

This is my code:


"varchar,varchar,varchar,integer",

[

{firstName="Bruce", lastName="Murray", userName="brucemurray", sumBoolean =
"#sumBoolean[1]#"},

{firstName="Micheal", lastName="Morecroft", userName="michealmorecroft",
sumBoolean = "#sumBoolean[2]#"},

{firstName="Dick", lastName="Weston", userName="dickweston", sumBoolean =
"#sumBoolean[3]#"},

{firstName="Fred", lastName="Sinner", userName="fredsinner", sumBoolean =
"#sumBoolean[4]#"},

{firstName="Don", lastName="Highland", userName="donhighland",sumBoolean =
"#sumBoolean[5]#"},

{firstName="Don", lastName="Highland", userName="donhighland",sumBoolean =
"#sumBoolean[6]#"},

{firstName="Peter",lastName="Began", userName="peterbegan",sumBoolean =
"#sumBoolean[7]#"},

{firstName="Henry",lastName="Dunes", userName="henrydunes",sumBoolean =
"#sumBoolean[8]#"}

]) >













qCandidate=QueryFilter(qTwoDimension, *function*(obj){

*return* obj.sumBoolean="#maxBoolean#"

})

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 10, 2022 Mar 10, 2022

Copy link to clipboard

Copied

This is my code:























component="FreshStructure_2018/component/boolean"

method="twoDimension"

returnvariable="sumBoolean" >






"varchar,varchar,varchar,integer",

[

{firstName="Bruce", lastName="Murray", userName="brucemurray", sumBoolean =
"#sumBoolean[1]#"},

{firstName="Micheal", lastName="Morecroft", userName="michealmorecroft",
sumBoolean = "#sumBoolean[2]#"},

{firstName="Dick", lastName="Weston", userName="dickweston", sumBoolean =
"#sumBoolean[3]#"},

{firstName="Fred", lastName="Sinner", userName="fredsinner", sumBoolean =
"#sumBoolean[4]#"},

{firstName="Don", lastName="Highland", userName="donhighland",sumBoolean =
"#sumBoolean[5]#"},

{firstName="Don", lastName="Highland", userName="donhighland",sumBoolean =
"#sumBoolean[6]#"},

{firstName="Peter",lastName="Began", userName="peterbegan",sumBoolean =
"#sumBoolean[7]#"},

{firstName="Henry",lastName="Dunes", userName="henrydunes",sumBoolean =
"#sumBoolean[8]#"}

]) >













qCandidate=QueryFilter(qTwoDimension, *function*(obj){

*return* obj.sumBoolean="#maxBoolean#"

})




Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Mar 11, 2022 Mar 11, 2022

Copy link to clipboard

Copied

What are the asterisks around "function" and "return"?

Your code is being mangled by the forum. Click on the "</>" button at the top of the reply text box, select "Java" as the language even though it is CF, and you will be able to include properly formatted code.

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 15, 2022 Mar 15, 2022

Copy link to clipboard

Copied

Here is an example that combines your code with the example in the QueryFilter documentation:

<cfscript>
	
	sumBoolean=arrayNew(1);
	
	sumBoolean[1]=1;
	sumBoolean[2]=2;
	sumBoolean[3]=3;
	sumBoolean[4]=5;
	sumBoolean[5]=8;
	sumBoolean[6]=13;
	sumBoolean[7]=21;
	sumBoolean[8]=34;
	
	cutOffBoolean=10;
	
    qTwoDimension = queryNew("firstName,lastName,userName,sumBoolean","Varchar,Varchar,Varchar,Integer", 
                [

					{firstName="Bruce", lastName="Murray", userName="brucemurray", sumBoolean =
					"#sumBoolean[1]#"},
					
					{firstName="Micheal", lastName="Morecroft", userName="michealmorecroft",
					sumBoolean = "#sumBoolean[2]#"},
					
					{firstName="Dick", lastName="Weston", userName="dickweston", sumBoolean =
					"#sumBoolean[3]#"},
					
					{firstName="Fred", lastName="Sinner", userName="fredsinner", sumBoolean =
					"#sumBoolean[4]#"},
					
					{firstName="Don", lastName="Highland", userName="donhighland",sumBoolean =
					"#sumBoolean[5]#"},
					
					{firstName="Don", lastName="Highland", userName="donhighland",sumBoolean =
					"#sumBoolean[6]#"},
					
					{firstName="Peter",lastName="Began", userName="peterbegan",sumBoolean =
					"#sumBoolean[7]#"},
					
					{firstName="Henry",lastName="Dunes", userName="henrydunes",sumBoolean =
					"#sumBoolean[8]#"}
					
				]);
				
    qCandidate=QueryFilter(qTwoDimension,function(obj){
    	
        // pick out rows whose sumBoolean is less than the cut-off value 
        return obj.sumBoolean < cutOffBoolean;
        
    })
    
    writeOutput("The filtered query (qCandidate) is:")
    writeDump(qCandidate)
</cfscript>

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 17, 2022 Mar 17, 2022

Copy link to clipboard

Copied

LATEST

@Alan4a , any joy with that code example?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation