在为 case-when 编写功能性 sequelize 查询时遇到问题

Having an issue writing a functional sequelize query for case-when

我正在尝试将此查询用于以下情况,但它不起作用:

[db.Sequelize.literal('case when "survey_session_question->scq"."eng_question_text" is not null then "survey_session_question->scq"."eng_question_text" else "survey_session_question->scq->base_question"."eng_question_text" end'), 'eng_question_text']

我应该得到字段 eng_question_text 不是 null 的值,但我仍然得到 eng_question_testnull 的值。

有人可以指出为什么它在这里没有按应有的方式工作吗?

编辑: 我编辑了查询,但它仍然无法正常运行,因为一些空问题和空 ID 在不应该出现在最终输出中。

有效的原始查询:

select case when scq.eng_question_text is not null then scq.eng_question_text 
                          else bcq.eng_question_text end as eng_question_text, 
                          ssa.answer_text as answer_comments, scq.id as survey_question_id
                          from survey_sessions ss
                          join survey_session_questions ssq on ssq.survey_session_id = ss.id
                          join survey_session_answers ssa on ssa.session_question_id = ssq.id
                          join survey_links sl on sl.id = ss.survey_link_id
                          join survey_custom_questions scq on scq.id = ssq.survey_question_id 
                          left join base_custom_questions bcq on bcq.id = scq.base_custom_question_id 
                          where ss.status_code = 'COMPLETE' and scq.survey_id = :surveyId
                          and ssa.answer_text is not null and ssa.answer_text != ''

我的续集版本-----

.findAll({
      attributes: [  
        [db.Sequelize.literal('"session_question->survey_session->survey_link->survey->client"."client_full_name"'), 'client_full_name'], 
        [db.Sequelize.literal('case when "session_question->scq"."eng_question_text" is not null then "session_question->scq"."eng_question_text" else "session_question->scq->base_question"."eng_question_text" end'), 'eng_question_text'],
        ['answer_text','answer_comments'],
        [db.Sequelize.literal('"session_question->scq"."id"'), 'survey_question_id']
      ],
      where:{
       [Op.and]: [
            {
              answer_text: { [Op.ne]: null }
            }
            , {
              answer_text: { [Op.ne]: '' }
            }
          ]
        }
       ,include: [
        {
          model: db.SurveySessionQuestion          
          , as: 'session_question'
          , attributes: []
          , include: 
          [
            {
              model: db.SurveySession             
              , as: 'survey_session'
              , attributes: []
              , where:{status_code:'COMPLETE'}
               , include:[
                  {
                    model: db.SurveyLink             
                    , as: 'survey_link'
                    , attributes: []
                    , include:
                    [
                      {
                        model: db.Survey             
                        , as: 'survey'
                        , attributes: []
                        , include:
                        [
                          {
                            model: db.Client             
                            , as: 'client'
                            , attributes: []
                          }
                        ]
                      }
                    ]
                  }
                ]
            },
            {
              model: db.SurveyCustomQuestion              
              , as: 'scq'
              , attributes: []
              , where: {
                survey_id: surveyId
              }
              , include:[
                {
                  model:db.BaseCustomQuestion
                  ,as:'base_question'
                  ,attributes: []
                }
              ]
            }
          ]
        }        
      ]
    });

生成了sql个查询-----

select
    "SurveySessionAnswer"."id",
    "session_question->survey_session->survey_link->survey->client"."client_full_name" as "client_full_name",
    case
        when "session_question->scq"."eng_question_text" is not null then "session_question->scq"."eng_question_text"
        else "session_question->scq->base_question"."eng_question_text"
    end as "eng_question_text",
    "SurveySessionAnswer"."answer_text" as "answer_comments",
    "session_question->scq"."id" as "survey_question_id"
from
    "survey_session_answers" as "SurveySessionAnswer"
left outer join ( "survey_session_questions" as "session_question"
inner join "survey_sessions" as "session_question->survey_session" on
    "session_question"."survey_session_id" = "session_question->survey_session"."id"
    and ("session_question->survey_session"."deletedAt" is null
        and "session_question->survey_session"."status_code" = 'COMPLETE')
left outer join "survey_links" as "session_question->survey_session->survey_link" on
    "session_question->survey_session"."survey_link_id" = "session_question->survey_session->survey_link"."id"
    and ("session_question->survey_session->survey_link"."deletedAt" is null)
left outer join "surveys" as "session_question->survey_session->survey_link->survey" on
    "session_question->survey_session->survey_link"."survey_id" = "session_question->survey_session->survey_link->survey"."id"
    and ("session_question->survey_session->survey_link->survey"."deletedAt" is null)
left outer join "clients" as "session_question->survey_session->survey_link->survey->client" on
    "session_question->survey_session->survey_link->survey"."client_id" = "session_question->survey_session->survey_link->survey->client"."id"
    and ("session_question->survey_session->survey_link->survey->client"."deletedAt" is null)
inner join "survey_custom_questions" as "session_question->scq" on
    "session_question"."survey_question_id" = "session_question->scq"."id"
    and ("session_question->scq"."deletedAt" is null
        and "session_question->scq"."survey_id" = '57d88936-eb2d-11eb-ae65-13e560ff06c2')
left outer join "base_custom_questions" as "session_question->scq->base_question" on
    "session_question->scq"."base_custom_question_id" = "session_question->scq->base_question"."id" ) on
    "SurveySessionAnswer"."session_question_id" = "session_question"."id"
    and ("session_question"."deletedAt" is null)
where
    ("SurveySessionAnswer"."deletedAt" is null
        and ("SurveySessionAnswer"."answer_text" is not null
            and "SurveySessionAnswer"."answer_text" != ''));```

所以我找到了如何从我的结果中删除空数据。 我在模型中添加了以下行:db.SurveySessionQuestion: 其中:{survey_question_id:{[Op.ne]:null}}

这从输出中取出了空值,并且在这之后似乎按要求运行的情况。