【问题标题】:create dummy column in a query that its value come from a condition在查询中创建其值来自条件的虚拟列
【发布时间】:2015-05-19 00:30:24
【问题描述】:

我正在准备一个查询,我将在 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 语句和 case 但运气不在我身边:(

【问题讨论】:

    标签: sql-server if-statement crystal-reports case dummy-data


    【解决方案1】:

    试试这个:

    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 
    

    【讨论】:

      猜你喜欢
      • 2023-03-16
      • 1970-01-01
      • 1970-01-01
      • 2023-01-17
      • 1970-01-01
      • 2014-11-13
      • 2014-08-19
      • 2011-04-16
      • 1970-01-01
      相关资源
      最近更新 更多