-1

我正在尝试将计算作为一列,但似乎失败了。没有称为间隙的列

这条线是

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

4

2 回答 2

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/

于 2013-04-26T05:36:33.783 回答
0

我得到的是你想要一个新的列gap,它还没有d,你可以在选择列表中这样介绍它

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

您不能引入具有别名的列,d.gap因为它不属于任何表。

所以你的完整查询是这样的

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

同样适用,d.metric_desc因为此列在您的表格中

于 2013-04-26T06:26:09.870 回答