On 2012-10-16 18:32, Denis McMahon wrote:
> On Mon, 15 Oct 2012 15:23:25 +0200, Lennart Jonsson wrote:
>
>> On 10/15/2012 02:03 PM, Denis McMahon wrote: [...]
>>> select nom as name, coalesce(id) as id from nomid group by name;
>
>> Whats the purpose of coalesce (in this context)?
>
> coalesce(id) and elt(1,id) will both return the first id that appears in
> the grouped list of ids for the name.
Ok, I think I see what you mean. coalesce(a_1, a_2, ..., a_n) is per
definition equal to:
case when a_1 is not null then a_1
when a_2 is not null then a_2
...
when a_n is not null then a_n
else null
end
in this case:
case when id is not null then id else null end
which is the same as:
id
But:
select id, name from T group by name
is illegal in sql92, sql99 loosens this restriction and allows the
construction iff id is f.d. of name. Mysql allows it by default (can be
fixed by via sanity properties). But, and this is why I asked:
select coalesce(id), name from T group by name
is equally illegal because coalesce is a function that maps one value
onto another (in contrast with for example min that maps a set of values
onto a value). Therefore:
select id, name from T group by name
could have been used instead
/Lennart