首页 > 解决方案 > 添加结合 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

所选列的数据类型如下所示。

对此有什么帮助吗?

标签: sqlsql-server

解决方案


推荐阅读