gpt4 book ai didi

具有筛选列的 PostgreSQL View

转载 作者:行者123 更新时间:2023-11-29 14:36:17 27 4
gpt4 key购买 nike

我有一张这样的 table :

date_added        owner        action
01-02-2016 1 note
04-02-2016 1 call
04-02-2016 1 call
05-02-2016 1 note
05-02-2016 1 meeting
06-02-2016 1 meeting
06-02-2016 1 note
06-02-2016 1 cal
06-02-2016 1 note
10-02-2016 1 call
10-02-2016 1 note
10-02-2016 1 meeting

我需要这样的 View :

date_added        owner        note        call        meeting
01-02-2016 1 1 0 0
04-02-2016 1 0 2 0
05-02-2016 1 0 0 1
06-02-2016 1 2 1 1
10-02-2016 1 1 1 1

我如何创建一个像这样的列

WHERE action LIKE 'note'

?

最佳答案

您可以使用CASE 表达式。

查询

select date_added, owner,
sum(case action when 'note' then 1 else 0 end) note,
sum(case action when 'call' then 1 else 0 end) call,
sum(case action when 'meeting' then 1 else 0 end) meeting
from your_table_name
group by date_added, owner;

Find demo here

关于具有筛选列的 PostgreSQL View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44195263/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com