tracker issue : CF-4200408

select a category, or use search below
(searches all categories and all time range)
Title:

Multiple order by statements add column to result

| View in Tracker

Status/Resolution/Reason: Closed/Won't Fix/BugVerified

Reporter/Name(from Bugbase): Sander Schoemaker / Sander Schoemaker ()

Created: 12/14/2017

Components: Database, Query-of-Query(IMQ)

Versions: 2016

Failure Type: Incorrectly functioning

Found In Build/Fixed In Build: Everything above 9,0,2,282541 /

Priority/Frequency: Normal / All users will encounter

Locale/System: ALL / Windows 7 64-bit

Vote Count: 1

Problem Description:
When you use this the query.cfc component to build a query of query (QoQ) of another ColdFusion Query and use order by on several colums, the last column in the order by list is added to the selected output.

Steps to Reproduce:
/* Create an unsorted CF-query */
unsorted = QueryNew("col1,col2,col3,col4","VarChar,VarChar,Integer,VarChar");
for (a=10;a gte 1;a--){
    QueryAddRow(unsorted);
    QuerySetCell(unsorted,"col1","col1 #a#");
    QuerySetCell(unsorted,"col2","col2 #a#");
    QuerySetCell(unsorted,"col3","#a#");
    QuerySetCell(unsorted,"col4","col4 #a#");
}

writeDump(var="#unsorted#");

/* Create a new CF query of query with the unsorted table */ 
sorted = new query(
    dbtype = "query"
    ,unsorted = unsorted
    ,sql = "select [col1],[col2] from unsorted order by [col3], [col4] asc"
    ).execute().getresult();

/* The last column in the order by list will be displayed in the result */  
writeDump(var="#sorted#", label="sorted"); 

The bug can also be reproduced here:
https://trycf.com/gist/79f6523d19efe6dcdb4da8872c1c2721/acf11?theme=monokai

Actual Result:
  	col1 	col2 	col4
1 	col1 1 	col2 1 	1
2 	col1 2 	col2 2 	2
3 	col1 3 	col2 3 	3
4 	col1 4 	col2 4 	4
5 	col1 5 	col2 5 	5
6 	col1 6 	col2 6 	6
7 	col1 7 	col2 7 	7
8 	col1 8 	col2 8 	8
9 	col1 9 	col2 9 	9
10 	col1 10 	col2 10 	10 

Expected Result:
  	col1 	col2
1 	col1 1 	col2 1
2 	col1 2 	col2 2
3 	col1 3 	col2 3
4 	col1 4 	col2 4
5 	col1 5 	col2 5
6 	col1 6 	col2 6
7 	col1 7 	col2 7
8 	col1 8 	col2 8
9 	col1 9 	col2 9
10 	col1 10 	col2 10

Any Workarounds:
You could split up the QoQ, running a second query after the first sort to select only the columns you need. This will however impact performance on large queries.

Attachments:

Comments:

As a follow-up, this is actually a bug in Query-of-Query, not Query.cfc. It can be reproduced using <cfquery> and queryExecute() as well. See this example: https://trycf.com/gist/258a56456c29bae7dfe5564376f1f0ef/acf2016?theme=monokai
Comment by Carl V.
165 | January 25, 2018 04:17:25 PM GMT
Bug is in the core query-of-query functionality, not just query.cfc.
Vote by Carl V.
166 | January 25, 2018 04:18:19 PM GMT