case when 和count
时间: 2023-10-26 08:28:27 浏览: 97
case when和sum case when 写法及拼接字段
引用:sum(case when onefield = 1 then 1 else 0 end) one_count 。引用:sum(case when onefield = 0 then 1 else 0 end) zero_count, 引用:1 case when 写法 2 sum case when 用法 3 select 拼接字段 示例如下: when 2 then 'C' else 'D' end ) as '类型',count(*) as '数量' from table group by orderType。
case when是一种用于条件判断的语法结构,在SQL查询中经常被使用。通过case when可以根据条件返回不同的值。在引用中,sum(case when onefield = 1 then 1 else 0 end)表示统计满足条件onefield等于1的记录数量,并将结果存储在one_count中。同理,在引用中,sum(case when onefield = 0 then 1 else 0 end)表示统计满足条件onefield等于0的记录数量,并将结果存储在zero_count中。而在引用中,case when被用于根据不同的条件返回不同的结果,并使用sum函数对结果进行求和,最后使用select语句进行字段拼接并按orderType进行分组统计。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* [sql 语句进阶操作(count、sum、case-when、group_concat、order by limit offset、dense_rank rank、exis...](https://blog.csdn.net/lyh_950622/article/details/107185718)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
- *3* [case when和sum case when 写法及拼接字段](https://download.csdn.net/download/u013716500/9540627)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文