在其值来自条件的查询中创建虚拟列

create dummy column in a query that its value come from a condition

正在准备一个查询,我将在 Crystal 报告中使用它 到目前为止,查询工作正常,但我想再添加一列(虚拟列),该列将根据条件进行填充, 这是查询:

select AcctCode
     , AcctName
     , Segment_0 + '-'+ Segment_1 as Acctnum
     , max(refdate) 
     , min(refdate)
     , sum(debit) as Debit
     , sum(credit)as Credit 
from oact t0 
  inner join jdt1 t1 on t0.acctcode = t1.Account
where (  Segment_0 LIKE '01%' 
      or segment_0 like '02%'  
      or Segment_0 like '03%'
      )  
      and 
      ( t0.Segment_1 = '01') 
      and  (refdate  between '2014-01-31' and '2015-12-27' )
group by AcctCode, AcctName,Segment_0, Segment_1
order by  AcctCode 

如果 Segment_0 以“01”开头,则虚拟列中的值将显示 'A' 如果 Segment_0 以 '02' 开头,则虚拟列中的值将显示 'L' 如果 Segment_0 以 '03' 开头,则虚拟列中的值将显示 'E' 我尝试使用 if 语句和 case 但运气不在我身边:(

试试这个:

select AcctCode
     , AcctName
     , Segment_0 + '-'+ Segment_1 as Acctnum
     , max(refdate) 
     , min(refdate)
     , sum(debit) as Debit
     , sum(credit)as Credit 
     , case when Segment_0 LIKE '01%' then 'A'
            when segment_0 like '02%' then 'L' 
            when Segment_0 like '03%' then 'E'
            else 'X'   
       end dummy_column
from oact t0 
  inner join jdt1 t1 on t0.acctcode = t1.Account
where (  Segment_0 LIKE '01%' 
      or segment_0 like '02%'  
      or Segment_0 like '03%'
      )  
      and 
      ( t0.Segment_1 = '01') 
      and  (refdate  between '2014-01-31' and '2015-12-27' )
group by AcctCode, AcctName,Segment_0, Segment_1
order by  AcctCode