首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在sql中使用case进行列的添加和填充?

如何在sql中使用case进行列的添加和填充?
EN

Stack Overflow用户
提问于 2021-06-07 18:26:25
回答 1查看 27关注 0票数 0

我有下面的表格

代码语言:javascript
复制
create table scientisttt (id integer, firstname varchar(100), lastname varchar(100));
        insert into scientisttt (id, firstname, lastname) values (1, 'albert', 'einstein');
        insert into scientisttt (id, firstname, lastname) values (2, 'isaac', 'newton');
        insert into scientisttt (id, firstname, lastname) values (3, 'marie', 'curie');
       insert into scientisttt (id, firstname, lastname) values (4, 'marie', 'curie');
        insert into scientisttt (id, firstname, lastname) values (5, 'isaac', 'newton');
        insert into scientisttt (id, firstname, lastname) values (6, 'isaac', 'newton');
        select * from scientisttt;

select count(*), firstname
from scientisttt
group by firstname;

select 
     * ,
     count(*) over (partition by firstname) as "Count"
into nam
from scientisttt; 

select * from nam;


select s.firstname, count into newtab from scientisttt as s left join namessss as n on s.firstname = n.firstname;

我知道如何创建一个包含额外列的新表,如下所示:

代码语言:javascript
复制
select *,
case 
when count >= 3 then 'ok'
else 'bad'
end as newcol
into newtab2
from newtab;

但是,有没有其他方法可以做到呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-06-07 18:57:42

您可以创建视图:

代码语言:javascript
复制
create view v_scientisttt as
    select s.* ,
           (case when count(*) over (partition by firstname) > 3
                 then 'ok' else 'bad'
            end) as count
    from scientisttt s;

使用该视图的任何人都可以看到正确的值。不需要为此创建新的表。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67869951

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档