Search code examples
sql-serverif-statementcrystal-reportscasedummy-data

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


am preparing a query which i will use it in Crystal Report the query is working fine so far but I want to add one more column (dummy column) which will be filled based on a condition , this is the query :

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 

if Segment_0 is start with '01' then the value in the dummy column will show 'A' if Segment_0 is start with '02' then the value in the dummy column will show 'L' if Segment_0 is start with '03' then the value in the dummy column will show 'E' i tried to use if statement, and case but luck wasn't in my side :(


Solution

  • try this:

    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