如果在SQL Select语句中使用IF THEN

时间:2019-04-16 16:30:06

标签: sql sql-server tsql

我正在写一份报告,以从日记帐交易中返回供应商名称。这是下面查询的基础。

我正在使用来自两个视图的数据:

  • 日记帐详细信息视图= libdwarf
  • 供应商详细信息视图= pa_journal_detail

匹配供应商编号:

供应商编号包含在以下字段中:

  1. ap_vendor
  2. ap_vendor.a_vendor_number [在如下所示的特定条件下]仅当日记帐源代码为“ API”或“ APM”时

源代码存储在pa_journal_detail.jl_ref1字段中

供应商名称存储在字段pa_journal_detail. jl_source_code

这是我开始的查询。当我尝试运行时,它返回不正确的语法错误。

ap_vendor.a_vendor_name

这是下面的完整查询。我也尝试过使用“ TOP 1”。我现在收到这样的错误:关键字AS附近语法不正确。

SELECT
CASE
          WHEN pa_journal_detail. jl_source_code = ‘API’
               OR pa_journal_detail. jl_source_code = ‘APM’
          THEN(
              SELECT  a_vendor_name
              FROM ap_vendor
              INNER JOIN pa_journal_detail 
              ON pa_journal_detail.jl_ref1 = ap_vendor.a_vendor_number)
          ELSE 0
  END as a_vendor_name, *
  FROM  pa_journal_detail

这就是我最终得到的结果,并且现在可以正常工作!谢谢大家!

SELECT
   pa_journal_detail.a_project
  ,pa_journal_detail.jl_seg2
  ,pa_journal_detail.jl_seg3
  ,pa_journal_detail.jl_seg4
  ,pa_journal_detail.jl_source_code
  ,pa_journal_detail.jl_ref1
  ,pa_journal_detail.jl_gl_org
  ,pa_journal_detail.jl_gl_obj
  ,pa_journal_detail.jl_line_num
  ,pa_journal_detail.jl_journal_num
  ,pa_journal_detail.jl_jnl_year_period
  ,pa_journal_detail.jl_journal_number
  ,pa_journal_detail.jl_journal_seq
  ,(SELECT(CASE
              WHEN pa_journal_detail. jl_source_code = 'API'
                   OR pa_journal_detail. jl_source_code = 'APM'
              THEN(
                  SELECT TOP 1 a_vendor_name 
                  FROM ap_vendor
                  RIGHT JOIN pa_journal_detail 
                  ON pa_journal_detail.jl_ref1 = ap_vendor.a_vendor_number
                 )
              ELSE 0
      END as 'a_vendor_name', *
      FROM  pa_journal_detail))
FROM pa_journal_detail

3 个答案:

答案 0 :(得分:1)

对于字符串比较,您需要使用单引号

 CASE
              WHEN pa_journal_detail. jl_source_code = 'API'
                   OR pa_journal_detail. jl_source_code = 'APM'
              THEN(
                  SELECT top 1  a_vendor_name -- here you need limit or top 1
                  FROM ap_vendor
                  INNER JOIN pa_journal_detail 
                  ON pa_journal_detail.jl_ref1 = ap_vendor.a_vendor_number
                 )
              ELSE 0
      END as a_vendor_name, *
      FROM  pa_journal_detail

答案 1 :(得分:0)

我认为case表达式可能是这项工作的错误工具。如果您想有时联接表,则left join可能会更容易:

SELECT    p.*, a.a_vendor_name
FROM      pa_journal_detail p
LEFT JOIN ap_vendor a ON p.jl_ref1 = a.a_vendor_number AND
          p.jl_source_code IN ('API', 'APM')

答案 2 :(得分:0)

还有iif()。我使用它的次数远远超过了我应该使用的次数,我只是希望为简单的条件工作而不是大型的Case语句提供一些帮助。

select iif(1 = 1,'True','False') 
-- any series that results in a boolean
select iif((1 = 1 and 0 = 0) and (5 / 1 = 5 and 5 % 10 = 5),'True','False')

用于查询

SELECT
   pa_journal_detail.a_project
  ,pa_journal_detail.jl_seg2
  ,pa_journal_detail.jl_seg3
  ,pa_journal_detail.jl_seg4
  ,pa_journal_detail.jl_source_code
  ,pa_journal_detail.jl_ref1
  ,pa_journal_detail.jl_gl_org
  ,pa_journal_detail.jl_gl_obj
  ,pa_journal_detail.jl_line_num
  ,pa_journal_detail.jl_journal_num
  ,pa_journal_detail.jl_jnl_year_period
  ,pa_journal_detail.jl_journal_number
  ,pa_journal_detail.jl_journal_seq
  ,iif(pa_journal_detail.jl_source_code = 'API' OR pa_journal_detail.jl_source_code = 'APM',(SELECT TOP 1 a_vendor_name 
                  FROM ap_vendor
                  RIGHT JOIN pa_journal_detail 
                  ON pa_journal_detail.jl_ref1 = ap_vendor.a_vendor_number)
                 ,0)
              'a_vendor_name', *
      FROM  pa_journal_detail))
FROM pa_journal_detail