sql - 添加结合 IN 运算符“操作数类型冲突:DATE 与 INT 不兼容”的 CASE 语句时出错
问题描述
我有一个 SQL 查询,它尝试创建关于时间的分组,例如 FISCAL Quarters 和 Fiscal Periods 等。
当我执行查询时,返回错误并显示以下消息
操作数类型冲突:日期与 int 不兼容
我多次尝试以不同的方式重写查询,但不幸的是,由于我对该主题没有足够的知识,我似乎无法弄清楚原因。但是,我确实认为错误是由于 CASE 语句与 IN 结合编写的。
我的代码在下面给出。
SELECT d1.*,
case when isnull(d1.rep_acc_period,0) in (1,2,3,4,5,6) then 'H1'
when isnull(d1.rep_acc_period,0) in (7,8,9,10,11,12) then 'H2'
else NULL
end AS FISCAL_HALF_YEAR_TXT,
case when isnull(d1.rep_acc_period,0) in (1,2,3,4,5,6) then 1
when isnull(d1.rep_acc_period,0) in (7,8,9,10,11,12) then 2
else NULL
end AS FISCAL_HALF_YEAR,
rep_acc_period as FISCAL_PERIOD,
cast (rep_acc_period as nvarchar) as FISCAL_PERIOD_TXT,
case when isnull(d1.rep_acc_period,0) in (1,2,3) then 'Q1'
when isnull(d1.rep_acc_period,0) in (4,5,6) then 'Q2'
when isnull(d1.rep_acc_period,0) in (7,8,9) then 'Q3'
when isnull(d1.rep_acc_period,0) in (10,11,12) then 'Q4'
else NULL
end AS FISCAL_QUARTER_TXT,
case when isnull(d1.rep_acc_period,0) in (1,2,3) then 1
when isnull(d1.rep_acc_period,0) in (4,5,6) then 2
when isnull(d1.rep_acc_period,0) in (7,8,9) then 3
when isnull(d1.rep_acc_period,0) in (10,11,12) then 4
else NULL
end AS FISCAL_QUARTER,
case when isnull(d1.rep_acc_period,0) in (1,2,3,4) then 1
when isnull(d1.rep_acc_period,0) in (5,6,7,8) then 2
when isnull(d1.rep_acc_period,0) in (9,10,11,12) then 3
else NULL
end AS FISCAL_TERTIAL,
rep_acc_year as FISCAL_YEAR,
rep_acc_year * 100 + rep_acc_period as FISCAL_YEAR_PERIOD,
id as IS_ID
FROM TIME_TAB d1
所选列的数据类型如下所示。
- rep_acc_period - bigint
- FISCAL_HALF_YEAR_TXT - varchar(2)
- rep_acc_period/FISCAL_PERIOD - bigint
- FISCAL_PERIOD_TXT - nvarchar
- FISCAL_QUARTER_TXT - varchar(2)
- FISCAL_QUARTER - 整数
- FISCAL_TERTIAL - 整数
- rep_acc_year/FISCAL_YEAR - bigint
- FISCAL_YEAR_PERIOD - bigint
对此有什么帮助吗?
解决方案
推荐阅读
- python - 下载需要从命令行单击“下载按钮”的文件
- python - pygame 图层,pygame.sprite
- javascript - 如何在快速路由器获取请求中从节点获取中使用获取
- kubernetes - 消息“错误:解析 mongodb-deployment.yaml 时出错:将 YAML 转换为 JSON 时出错:yaml:第 27 行:未找到预期的密钥”
- visual-studio-code - 如何配置“--allow-insecure-localhost”和“--ignore-certificate-errors”?
- java - 如何为多线程代码执行时间编写测试
- random - 如何在 python turtle 模块中生成随机大数量
- flutter - 如何防止带有回调的子树在 Flutter 中重新渲染?
- python - 如何从列表中更改 API 中的变量并创建数据框
- python - 我想从字符串中提取所有十进制数字,我该怎么做?