0

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

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 语句和大小写,但运气不在我身边:(

4

1 回答 1

0

试试这个:

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 
于 2015-03-16T19:12:12.243 回答