SQL:保留 CASE + GROUP BY (+ VIEW) 中的原始列名 - 不明确的列名

mkc*_*zyk 3 sql postgresql group-by case aggregate-functions

当我根据条件从许多表中获取列并且由于使用了聚合函数CASE而必须使用此名称时,如何保留原始列名称?GROUP BY我需要它来创建VIEW正确的列名称。我使用 PostgreSQL。

例子

我有三张桌子:

create table first
(
    id    bigserial primary key,
    field varchar(255) not null
);

create table second
(
    id       bigserial primary key,
    field    varchar(255) not null,
    first_id bigint constraint second_first_id references first
);

create table values
(
    id       bigserial primary key,
    value    numeric,
    first_id bigint constraint value_first_id references first
);
Run Code Online (Sandbox Code Playgroud)

一些输入数据:

insert into first (field) values ('aaa');
insert into first (field) values ('bbb');
insert into second (field, first_id) values ('ggg', 1);
insert into second (field, first_id) values ('hhh', 2);
insert into values (value, first_id) values (2, 1);
insert into values (value, first_id) values (4, 1);
insert into values (value, first_id) values (20, 2);
insert into values (value, first_id) values (40, 2);
Run Code Online (Sandbox Code Playgroud)

以及连接这些表和分组依据的 SQL:

select sum(v.value) as sum_value,
       f.field,
       s.field
from first f
         left join second s on f.id = s.first_id
         left join values v on f.id = v.first_id
group by f.field, s.field;
Run Code Online (Sandbox Code Playgroud)

现在我添加CASE带有某些条件的语句(为了简单起见)以从表中true获取字段:fieldfirstsecond

select sum(v.value) as sum_value,
       case
           when true then
               f.field
           else
               s.field
           end      as field
from first f
         left join second s on f.id = s.first_id
         left join values v on f.id = v.first_id
group by field;
Run Code Online (Sandbox Code Playgroud)

有错误:

ERROR: column reference "field" is ambiguous
Run Code Online (Sandbox Code Playgroud)

as field我可以通过更改并group by field使用新名称表单 example来修复错误field1

但我想保留原来的名字field。当我创建视图时它很重要:

create or replace view my_view as
select sum(v.value) as sum_value,
       case
           when true then
               f.field
           else
               s.field
           end      as field1
from first f
         left join second s on f.id = s.first_id
         left join values v on f.id = v.first_id
group by field1;

select * from my_view;
Run Code Online (Sandbox Code Playgroud)

该视图返回带有名称field1而不是 的列field

我尝试as field通过以下方式限定视图中的名称group by my_view.field,但它也不起作用:

ERROR: missing FROM-clause entry for table "my_view"
Run Code Online (Sandbox Code Playgroud)

我可以复制整个CASE语句并将其粘贴进去GROUP BY,但我认为这不是一个好的解决方案(特别是当CASE很长且有很多列时):

create or replace view my_view as
select sum(v.value) as sum_value,
       case
           when false then
               f.field
           else
               s.field
           end      as field
from first f
         left join second s on f.id = s.first_id
         left join values v on f.id = v.first_id
group by case
             when false then
                 f.field
             else
                 s.field
             end;
Run Code Online (Sandbox Code Playgroud)

我看到了这些问题:12,它不是重复的。

Cet*_*soz 5

您可以在分组依据中使用列的序号。IE:

select sum(v.value) as sum_value,
       case
           when true then
               f.field
           else
               s.field
           end      as field
from first f
         left join second s on f.id = s.first_id
         left join values v on f.id = v.first_id
group by 2;
Run Code Online (Sandbox Code Playgroud)