如何为单个列中存在的值添加多个条件并将结果显示在单独的列中?

Posted

技术标签:

【中文标题】如何为单个列中存在的值添加多个条件并将结果显示在单独的列中?【英文标题】:How to add multiple conditions for the values present in a single column and display the result in separate columns? 【发布时间】:2021-04-19 14:54:53 【问题描述】:

我的表格结构如下。

number status
123 failed
456 failed
123 success
789 failed
789 success
456 success
123 success

我需要编写一个查询,结果如下:

number total_occurence failed_count success_count
123 3 1 2
456 2 1 1
789 2 1 1

【问题讨论】:

请不要贴截图,请回滚到文字 【参考方案1】:

你可以这样做:

select number 
       , count(*) total
       , count(case when status = 'fail' then 1 end) fail_count
       , count(case when status = 'success' then 1 end) success_count
from yourtable
group by number

【讨论】:

谢谢,兄弟!它就像一个魅力。您能否推荐我可以详细学习 SQL 的资源。我知道 SQL 但我的知识很浅。 @SyedKamranAhmed 不客气,那里有很多免费资源,如果你用谷歌搜索,我不熟悉任何具体的

以上是关于如何为单个列中存在的值添加多个条件并将结果显示在单独的列中?的主要内容,如果未能解决你的问题,请参考以下文章