MYSQL双嵌套SELECT

时间:2012-09-04 13:57:18

标签: mysql database select

我有一个'双'嵌套SELECT语句。更深层的语句提取了我想要插入到现有行中的每月总数所需的值。但是每个“Segment”具有不同的百分比,因此它会产生多行。为了解决这个问题,我有一个外部选择,然后对结果进行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; 

0 个答案:

没有答案