2013-04-26 2 views
-1

나는 열로 계산을하려고하지만, .There를 실패하는 것하고는 차이라는 럼없는케이스 SQL 컬럼 계산

라인은 전체 스크립트는

case d.gap when a.actual_value IS TRUE then (quar_target - a.actual_value) else 'NULL' END , 

입니다

SELECT  
    weekly.* , 
    quarterly.target_value as quar_target 
FROM ( 
    SELECT a.week_id, 
      d.region_id, 
      d.region_name, 
      d.metric_id , 
      case d.metric_desc 
       when 'BE GMV Lift' then 'GMV Lift' 
       when 'B2C GMV Lift' then 'GMV Lift' 
       when 'Trust GMV Lift' then 'GMV Lift' 
       else d.metric_desc 
      end as metric_desc, 
      case d.gap 
       when a.actual_value IS TRUE 
        then (quar_target - a.actual_value) 
       else 'NULL' END, 
      d.ini_name  , 
      a.actual_value , 
      a.actual_txt , 
      a.target_value , 
      a.target_txt , 
      a.signals  , 
      a.comments      
    FROM  
     -- Get most recently reported records. If the metric is not reported for this week, get the last reported number 
     (SELECT * 
      FROM l1_weekly_entry 
      WHERE week_id=WEEK(CURDATE(), 1) - 1 
     ) a  

열을 소개하려고합니다. d.gap

+1

는 "하지만 실패 할 것"실제로 어떤 의미가 테이블에 있기 때문에

그래서 전체 쿼리 d.metric_desc 근무 같은이

SELECT weekly.* , quarterly.target_value as quar_target FROM ( SELECT a.week_id, d.region_id, d.region_name, d.metric_id , case d.metric_desc when 'BE GMV Lift' then 'GMV Lift' when 'B2C GMV Lift' then 'GMV Lift' when 'Trust GMV Lift' then 'GMV Lift' else d.metric_desc end as metric_desc, gap = case when a.actual_value IS TRUE then (quar_target - a.actual_value) else 'NULL' END, d.ini_name , a.actual_value , a.actual_txt , a.target_value , a.target_txt , a.signals , a.comments FROM -- Get most recently reported records. If the metric is not reported for this week, get the last reported number (SELECT * FROM l1_weekly_entry WHERE week_id=WEEK(CURDATE(), 1) - 1 ) a 

같은 것? –

+0

귀하의 별칭이 될 수 있습니다 "d"는 열 "갭"이 없다는 것을 확인하십시오 – KuldipMCA

+0

예 !! # 1054 - 필드 목록에서 알 수없는 'd.gap'열 – user2300435

답변

0

당신에없는 새 열 gap을 갖고 싶어 d, 선택 사항 l에서 이와 같이 소개 할 수 있습니다. 이 모든 테이블에 속하지 않기 때문에 IST는

..., gap = (case when a.actual_value IS TRUE 
        then (quar_target - a.actual_value) 
       else 'NULL' END), ... 

당신은 d.gap 같은 별칭이있는 열을 소개 할 수 없습니다. 이 열이

0

답안의 많은 곳에서 쓰기 위해 END 글을 쓰는 것을 잊고 있습니다. 예 :

case d.gap when a.actual_value IS TRUE then (quar_target - a.actual_value) else 'NULL' END 

마찬가지로.

MSDN : 부여 됨으로써으로

http://msdn.microsoft.com/en-us/library/ms181765.aspx

구문 : 내가 얻고 것은

http://blog.sqlauthority.com/2007/04/14/sql-server-case-statementexpression-examples-and-explanation/

+0

# 1054 - '필드 목록에서 알 수없는'd.gap '열이 있는데이 오류가 발생합니다. – user2300435

+0

이것은 단순히 alies d에 열 간격이 없음을 의미합니다. 그것을위한 테이블 구조를보십시오. – Freelancer

+0

차이 대상을 수행하는 새로운 열을 생성하기 만하면됩니다. 실제는 표의 열입니다. – user2300435