Question

I have a query object, with, say, fifteen rows returned. For all intents and purposes, I can't modify the SQL which generates the query object, but I need to sort this query object by column. Is there a way to do this in ColdFusion 7 without resorting to an external library?

Edit: I should add: I've run a query on this query object, and done an ORDER BY clause in this query of the query. Is there another way of doing this?

Was it helpful?

Solution

No, a query of query is the way you would do this. There are other ways you could monkey around with the data, but they're all kludgey and wouldn't be as straightforward as QoQ.

One of the powers of QoQ (aka in-memory queries) is that it can be used on the query returned by any tag that returns a query object, for instance CFDIRECTORY and CFPOP.

For folks wondering how to do a Query of Query, it's simple:

<cfquery name="resortQuery" dbtype="query">
    SELECT *
    FROM myQueryFromSomewhereElse
    WHERE
        COLUMN_NAME=<cfqueryparam cfsqltype="CF_SQL_VARCHAR" value="#myFilterValue#" />
    ORDER BY
        SOME_OTHER_COLUMN_NAME ASC
</cfquery>

OTHER TIPS

Even when this question is already solved I want to add, that you could also use the underlying Java method sort(), which is just needing one line and you don't need to add a UDF for this. The code would then look like this:

<cfset qQuery.sort(qQuery.findColumn("nameOfSortColumn"), TRUE)>

The findColumn() call is needed to get the index of the sort column, because sort() is working with the column indexes and not with the column names. The second parameter specifies the sort order: TRUE = ascending, FALSE = descending.

Advantages: one-line call, faster than QoQ

Disadvantage: sort restricted to one column

There are much more hidden features of the underlying Java class. See the following links for more information:

In Lucee (tested with 4.5 and 5.2) this also works similar, and even simpler:

<cfset qQuery.sort("nameOfSortColumn", "asc")>

Hope, this gives some ideas...

Please check the next url

http://www.coldfusioncookbook.com/entries/How-do-I-resort-a-query.html

<cfquery name="original" datasource="foo" >
  select name, age, rank
  from people
  order by age asc
</cfquery>

<!--- Resort by name --->
<cfquery name="newQuery" dbtype="query">
  select name, age, rank
  from original
  order by name asc
</cfquery>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top