Subject Re: [firebird-support] What's wrong with this SQL?
Author Thomas Steinmaurer
> select
> case when "RemovedAt" is null then 'Cur' else 'Hist' end "Category",
> count(*) "Count"
> from "Objects"
> where "ObjectKind" = 92
> group by case when "RemovedAt" is null then 'Cur' else 'Hist' end
> order by case when "RemovedAt" is null then 'Cur' else 'Hist' end
>
> Why would that SQL give me this error:
>
> SQL Message : -104
> Invalid token
>
> Engine Code : 335544569
> Engine Message :
> Dynamic SQL Error
> SQL error code = -104
> Invalid expression in the select list (not contained in either an
> aggregate function or the GROUP BY clause)
>
> I've copied and pasted the group by clause into both order by and
> select, so I think it should be alright.

I'm not sure, if this is a bug in Firebird, but depending on your
Firebird version, you could group/order by the alias name or field index
(if it isn't generated by ECO).


select
case when "RemovedAt" is null then 'Cur' else 'Hist' end "Category",
count(*) "Count"
from "Objects"
where "ObjectKind" = 92
group by "Category"
order by "Category"

or

select
case when "RemovedAt" is null then 'Cur' else 'Hist' end "Category",
count(*) "Count"
from "Objects"
where "ObjectKind" = 92
group by 1
order by 1


Hope this helps.

--
With regards,
Thomas Steinmaurer

* Upscene Productions - Database Tools for Developers
http://www.upscene.com/

* My Blog
http://blog.upscene.com/thomas/index.php

* Firebird Foundation Committee Member
http://www.firebirdsql.org/en/firebird-foundation/