条件格式新增列case when
时间: 2023-08-19 17:13:30 浏览: 93
条件格式新增列case when可以在SELECT语句中使用CASE WHEN语句来创建一个新的列。例如,可以使用以下语法来实现:
SELECT column_1, column_2, CASE WHEN condition_1 THEN 'A' WHEN condition_2 THEN 'B' ELSE 'C' END AS new_column
FROM table_name
WHERE condition_3 = 'xxx';
在这个例子中,根据满足的条件,CASE WHEN语句会返回不同的值,并将其作为新的列添加到查询结果中。如果condition_1满足,则新列的值为'A',如果condition_2满足,则新列的值为'B',否则新列的值为'C'。[3]
相关问题
mybatisplus case when
Mybatis-plus中的CASE WHEN语句用于在查询中进行条件判断和返回结果的处理。引用和引用中的代码示例展示了如何在Mybatis-plus中使用CASE WHEN语句。在这个示例中,通过使用QueryWrapper的apply方法结合CASE WHEN语句,可以将原本的SQL语句改写为Mybatis-plus的查询语法。
具体来说,可以通过在apply方法中使用CASE WHEN语句定义条件判断和结果返回的逻辑。例如,可以根据不同的push_rate值来计算并返回push_time字段的值。
在QueryWrapper中使用apply方法时,可以自由书写SQL语句,通过拼接字符串的方式进行条件判断和结果返回的定义。在拼接字符串时,可以使用双引号来拼接多行字符串,以提高代码的可读性。
总结来说,Mybatis-plus中的CASE WHEN语句可以通过QueryWrapper的apply方法来实现条件判断和结果返回的逻辑定义。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* [Mybatis-plugs 批量新增及批量修改、IN、CASE WHEN操作](https://blog.csdn.net/qq_21377917/article/details/105167847)[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^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
- *2* *3* [mybatisPlus使用case when代码示例](https://blog.csdn.net/weixin_44060488/article/details/131001948)[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^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
select a.month_id, sum(b.aa) aa, sum(b.bb) bb, sum(b.cc) cc, '今年往月新增订购' as A from ( select month_id, sum(aa) aa,sum(bb) bb,sum(cc) cc from ( select month_id , count(distinct case when offer_type_name='主套餐' then offer_name end) aa, count(distinct case when offer_type_name='促销' then offer_name end) bb, count(distinct case when offer_type_name='可选包' then offer_name end) cc, from dwd_sum_zhuliuxxsc where substring(crt_version,1,4)='2023' group by month_id ) ptuser group by month_id ) a join ( select month_id,sum(aa) aa,sum(bb) bb,sum(cc) cc from ( select month_id , count(distinct case when offer_type_name='主套餐' then offer_name end) aa, count(distinct case when offer_type_name='促销' then offer_name end) bb, count(distinct case when offer_type_name='可选包' then offer_name end) cc, from dwd_sum_zhuliuxxsc where substring(crt_version,1,4)='2023' group by month_id ) ptuser group by month_id ) b on a.month_id >= b.month_id group by a.month_id order by a.month_id;如何改成计算今年往月(不包含本月)的统计
您可以将子查询中的 `where substring(crt_version,1,4)='2023'` 改为 `where year(crt_version) = year(current_date()) and month_id < month(current_date())`,这样就只会统计今年往月的数据(不包含本月)。修改后的 SQL 语句如下:
```
select a.month_id,
sum(b.aa) aa,
sum(b.bb) bb,
sum(b.cc) cc,
'今年往月新增订购' as A
from (
select month_id, sum(aa) aa,sum(bb) bb,sum(cc) cc
from (
select month_id,
count(distinct case when offer_type_name='主套餐' then offer_name end) aa,
count(distinct case when offer_type_name='促销' then offer_name end) bb,
count(distinct case when offer_type_name='可选包' then offer_name end) cc
from dwd_sum_zhuliuxxsc
where year(crt_version) = year(current_date()) and month_id < month(current_date())
group by month_id
) ptuser
group by month_id
) a
join (
select month_id,sum(aa) aa,sum(bb) bb,sum(cc) cc
from (
select month_id,
count(distinct case when offer_type_name='主套餐' then offer_name end) aa,
count(distinct case when offer_type_name='促销' then offer_name end) bb,
count(distinct case when offer_type_name='可选包' then offer_name end) cc
from dwd_sum_zhuliuxxsc
where year(crt_version) = year(current_date()) and month_id < month(current_date())
group by month_id
) ptuser
group by month_id
) b
on a.month_id >= b.month_id
group by a.month_id
order by a.month_id;
```
注意,这里的 `current_date()` 函数是 MySQL 中获取当前日期的函数,如果您使用的是其他数据库,获取当前日期的函数可能有所不同。
阅读全文