Обсуждение: confusing `case when` column name

Поиск
Список
Период
Сортировка

confusing `case when` column name

От
"adjkldd@126.com"
Дата:
Hi hackers,

Below is a `case when` demo,

```sql
create table foo(a int, b int);
insert into foo values (1, 2);
select case 1 when 1 then a else b end from foo;
```

Currently, psql output is,

```text
b --- 1 (1 row)
```

At the first glance at the output column title, I assume the result of the sql is wrong. It should be `a`.
After some investigation, I discovered that the result's value is accurate. However, PostgreSQL utilizes b as the title for the output column.
Nee we change the title of the case-when output column? If you hackers think it's worth the effort, I'm willing to invest time in working on it.
Best Regards,
Winter Loo

Re: confusing `case when` column name

От
"David G. Johnston"
Дата:
On Tuesday, March 12, 2024, adjkldd@126.com <winterloo@126.com> wrote:

Nee we change the title of the case-when output column?

Choosing either a or b as the label seems wrong and probably worth changing to something that has no meaning and encourages the application of a column alias.

David J.
 

Re: confusing `case when` column name

От
Tom Lane
Дата:
"David G. Johnston" <david.g.johnston@gmail.com> writes:
> On Tuesday, March 12, 2024, adjkldd@126.com <winterloo@126.com> wrote:
>> Nee we change the title of the case-when output column?

> Choosing either a or b as the label seems wrong and probably worth changing
> to something that has no meaning and encourages the application of a column
> alias.

Yeah, we won't get any kudos for changing a rule that's stood for
25 years, even if it's not very good.  This is one of the places
where it's just hard to make a great choice automatically, and
users are probably going to end up applying an AS clause most of
the time if they care about the column name at all.

            regards, tom lane



Re: confusing `case when` column name

От
Daniel Gustafsson
Дата:
> On 12 Mar 2024, at 15:19, Tom Lane <tgl@sss.pgh.pa.us> wrote:

> users are probably going to end up applying an AS clause most of
> the time if they care about the column name at all.

If anything, we could perhaps add a short note in the CASE documentation about
column naming, the way it's phrased now a new user could very easily assume it
would be "case".

--
Daniel Gustafsson