sql - 我如何在配置单元中为这个用例创建一个宏?
问题描述
我有以下带有重复 case when 语句的查询,我想为此创建宏
select
*,
case when s1 between 0 and 40 then 'low'
when s1 between 41 and 80 then 'medium'
when s1 between 81 and 100 then 'high'
end as first_segment ,
case when s2 between 0 and 40 then 'low'
when s2 between 41 and 80 then 'medium'
when s2 between 81 and 100 then 'high'
end as s_segement
from
table
我试过了,但它不起作用。其中 x 是一列
CREATE TEMPORARY MACRO bucket(x INT)
case when x between 0 and 40 then 'low'
when x between 41 and 80 then 'medium'
when x between 81 and 100 then 'high'
end as x+'_'+'bucket';
INT 是指列类型的内容吗?上面的宏不起作用..
ParseException line 5:8 missing EOF at 'as' near 'end'
解决方案
在查询中添加宏别名:
CREATE TEMPORARY MACRO bucket(x INT)
case when x between 0 and 40 then 'low'
when x between 41 and 80 then 'medium'
when x between 81 and 100 then 'high'
end;
select bucket(s1) as first_segment,
bucket(s2) as s_segement,
from ...
推荐阅读
- c++ - Vim 没有正确缩进我的 C/C++ 文件
- r - 面向非 R 用户的交互式 R 笔记本/仪表板
- c# - Powershell Invoke() 在 C# 中为螺栓命令运行返回零
- reactjs - 如何将我的本地 React 目录挂载到我的 React docker 容器中?
- vue.js - 为什么创建的方法在 vue-resources 中不起作用?
- html - 从列表中获取特定链接
- python - 如何在 Python 上上线 bot
- php - 将基本 URL 更改为另一个 URL
- python - BS4 - 后代返回空,尽管它是错误的
- spring - Spring Batch - 复合项目编写器,列表作为编写器的输入