Skip to main content
nikos101
Inspiring
September 19, 2008
Question

If I want to order by dateAdded to I have to include it in the select statement like

  • September 19, 2008
  • 5 replies
  • 640 views
If I do this:

<cfquery name="chart" datasource="#datasource#">
select behaviourID,score, dateAdded2 = convert(varchar, dateAdded , 101)
from staff_charts_data

where userID =
<cfqueryparam value="#arguments.userID#" cfsqltype="cf_sql_integer"> AND
NOT score = 5
</cfquery>

If I want to order by dateAdded to I have to include it in the select statement like so:

<cfquery name="chart" datasource="#datasource#">
select behaviourID,score, dateAdded2 = convert(varchar, dateAdded , 101),dateAdded
from staff_charts_data

where userID =
<cfqueryparam value="#arguments.userID#" cfsqltype="cf_sql_integer"> AND
NOT score = 5
</cfquery>

This topic has been closed for replies.

5 replies

Inspiring
September 19, 2008
You could be right. I cannot remember whether that one is across the board. I know GROUP BY is different. IIRC MySql will let you group by a column that is not in the select list, but MS SQL does not.
Inspiring
September 19, 2008
> The only time the order by field has to be in the select
> clause is when your query has an aggregate and group
> by clause, like this:

No. It depends on the database. In MS SQL the restriction also applies when you use SELECT DISTINCT.
Inspiring
September 19, 2008
quote:

Originally posted by: -==cfSearching==-

No. It depends on the database. In MS SQL the restriction also applies when you use SELECT DISTINCT.

Maybe it's all databases. I just tried a query with redbrick and got an error. I don't think I've ever found out in my usual manner before, accidentally.
nikos101
nikos101Author
Inspiring
September 19, 2008
I'm impressed by your level of expertise :) Dan
Inspiring
September 19, 2008
you should be able to order by dateAdded without it being in the select clause. The only time the order by field has to be in the select clause is when your query has an aggregate and group by clause, like this:

select field1, field2, max(field3) as youralias
from sometables
where whatever
group by field1, field2
order by ???

You can only order field1, field2, max(field3) or youralias. You can't order by field4.
Inspiring
September 19, 2008
Did you have a question?