如何基于sql中的文本数据生成计数

ndh0cuux  于 2021-07-24  发布在  Java
关注(0)|答案(2)|浏览(274)

我有下表:

我基本上希望透视status列的内容,并生成'issuer/ticker'列的计数。例如,我想知道对于allison shenoy,过期和3个月内到期的“发行人/股票发行人”的数量是多少。因此,我的答案应该是:过期:11和3个月内到期:8基于上述数据。

z4iuyo4d

z4iuyo4d1#

使用条件聚合:

select
    assigned_analyst,
    sum(case when status = 'Past Due' then 1 else 0 end) past_due,
    sum(case when status = 'Due within 3 Months' then 1 else 0 end) due_3_month
from mytable
group by assigned_analyst
pobjuy32

pobjuy322#

要获得基于每个分析师和状态的发行人/股票发行人计数,请尝试以下操作:

SELECT
    [Assigned Analyst]
    ,[Status]
    COUNT(distinct [Issuer/Ticker]) [Count of Issuer/Ticker]
FROM TableName
GROUP BY [Assigned Analyst], [Status]

否则,要仅获取allison shenoy的计数,请尝试以下操作:

SELECT
    [Status]
    COUNT(distinct [Issuer/Ticker]) [Count of Issuer/Ticker]
FROM TableName
WHERE [Assigned Analyst] = 'Allison Shenoy'
GROUP BY [Status]

显然,用表的实际名称替换“tablename”

相关问题