0

我有一个“双重”嵌套的 SELECT 语句。更深层次的语句提取了我尝试插入现有行的每月总计所需的值。但是,每个“段”都有不同的百分比,因此它会导致多行。为了解决这个问题,我有一个外部选择,然后对结果进行 SUM。

更多问题是我无法将结果与月份匹配,因为当我尝试链接到列名称“Enrol_month_short”时,我收到以下消息。

“‘where 子句’中的未知列‘Enrol_month_short’”

SELECT Date_format(tbl_client_details.cd_enrol_date, '%b %y')AS 
       Enrol_month_short, Sum(tbl_revenue.rev_value) AS 
       Enrol_Cum_Rev, (SELECT Sum(tbl_revenue.rev_value) 
       FROM tbl_client_details 
       LEFT OUTER JOIN gridlock.tbl_revenue 
       ON tbl_client_details.cd_zbi_no = tbl_revenue.rev_zbi_no 
       AND tbl_client_details.cd_ucn = tbl_revenue.rev_ucn 
       WHERE ( Date_format(tbl_client_details.cd_enrol_date, '%b %y') = 
       enrol_month_short ) 
       AND ( Date_format(rev_month, '%b %y') = enrol_month_short ) 
       GROUP  BY Date_format(tbl_client_details.cd_enrol_date, '%b %y')) AS 
       New_client_rev, 
       (SELECT Count(tbl_client_details.cd_id) 
       FROM   tbl_client_details 
       WHERE  ( tbl_client_details.cd_enrol_date >= '2012-04-01' 
                AND tbl_client_details.cd_enrol_date <= '2012-07-31' ) 
                AND Date_format(tbl_client_details.cd_enrol_date, '%b %y') = 
                enrol_month_short 
       GROUP  BY Date_format(tbl_client_details.cd_enrol_date, '%b %y')) AS 
       Actual_Enrols, 
       (SELECT ( ( Sum(tbl_revenue.rev_value) * 
               ( tbl_segment.seg_growth_2012 / 100 ) ) / 12 ) AS YEAR_NEW_BUS_BUDGET 
       FROM tbl_client_details 
       LEFT OUTER JOIN tbl_revenue 
       ON tbl_client_details.cd_zbi_no = tbl_revenue.rev_zbi_no 
       AND tbl_client_details.cd_ucn = tbl_revenue.rev_ucn 
       LEFT OUTER JOIN tbl_segment 
       ON tbl_client_details.cd_segment = tbl_segment.seg_name 
       WHERE ( rev_month >= '2012-04-01' AND rev_month <= '2012-07-31' ) 
       GROUP  BY enrol_month_short)                                      AS 
       YEAR_NEW_BUS_BUDGET 
       FROM tbl_client_details 
       LEFT OUTER JOIN gridlock.tbl_revenue 
       ON tbl_client_details.cd_zbi_no = tbl_revenue.rev_zbi_no 
       AND tbl_client_details.cd_ucn = tbl_revenue.rev_ucn 
       WHERE ( tbl_client_details.cd_enrol_date >= '2012-04-01' 
       AND tbl_client_details.cd_enrol_date <= '2012-07-31' ) 
       AND ( rev_month >= '2012-04-01' 
       AND rev_month <= '2012-07-31' 
       OR rev_month = NULL ) 
       GROUP BY enrol_month_short 
       ORDER BY tbl_client_details.cd_enrol_date; 
4

0 回答 0