3

在下面的查询中,如何将别名assignedTOiti, assignedTOasstAdm, assignedTOhlp,添加closedDate到 group by 子句。

在过去的几个小时里,我一直在挣扎。

SELECT ieor.aper07_req_k                                  AS reqno,
       ford.apeh09_supervisor_cds_d                       AS ll6CdsID,
       'Ford Offboard Req'                                AS reqtype,
       par_val1.aper17_parameter_val_x                    AS requeststatus,
       (SELECT Min(iti2.aper07_req_created_s)
        FROM   iaper07_employee_offbrd_req iti2
        WHERE  ieor.aper07_req_k = iti2.aper07_req_k
               AND iti2.aper06_req_status_k = 4)          AS assignedTOiti,
       (SELECT Min(hlp2.aper07_req_created_s)
        FROM   iaper07_employee_offbrd_req hlp2
        WHERE  ieor.aper07_req_k = hlp2.aper07_req_k
               AND hlp2.aper06_req_status_k = 5)          AS assignedTOasstAdm,
       (SELECT Min(asst2.aper07_req_created_s)
        FROM   iaper07_employee_offbrd_req asst2
        WHERE  ieor.aper07_req_k = asst2.aper07_req_k
               AND asst2.aper06_req_status_k = 12)        AS assignedTOhlp,
       (SELECT Min(clsd2.aper07_req_created_s)
        FROM   iaper07_employee_offbrd_req clsd2
        WHERE  ieor.aper07_req_k = clsd2.aper07_req_k
               AND clsd2.aper06_req_status_k IN ( 3, 9 )) AS closedDate,
       ieor.aper07_assigned_to_cds_d,
       ieor.aper07_comment_x,
       ieor.aper07_last_updt_user_c,
       ieor.aper07_last_updt_s                            AS last,
       ieor.apeh09_employee_k,
       ieor.aper06_req_status_k,
       ipw.apeh04_first_n,
       ipw.apeh04_cds_d,
       ipw.apeh04_last_n,
       ipw.apeh04_person_type_c,
       irs.aper06_req_status_x,
       offr.aper15_offboard_reason_x                      AS offboardReason,
       Min(aper07_req_created_s)                          AS creationDate
FROM   iaper07_employee_offbrd_req ieor,
       iapeh05_person_vw ipw,
       iapeh09_ford_employee ford,
       iaper06_req_status irs,
       iaper15_offbrd_req_reason offr,
       iaper17_parameter_values par_val1
WHERE  ford.apeh09_cds_d = ipw.apeh04_cds_d
       AND ieor.apeh09_employee_k = ipw.apeh04_visitor_k
       AND irs.aper06_req_status_k = ieor.aper06_req_status_k
       AND offr.aper15_offboard_reason_k = ieor.aper15_offboard_reason_k
       AND ieor.aper06_req_status_k = par_val1.aper17_parameter_val_r
       AND ipw.apeh22_region_k = 1
       AND ieor.aper07_last_updt_s = (SELECT Max(aper07_last_updt_s)
                                      FROM   iaper07_employee_offbrd_req b
                                      WHERE  ieor.aper07_req_k = b.aper07_req_k)
       AND ieor.aper07_req_k = 3387
GROUP  BY ieor.aper07_req_k,
          ford.apeh09_supervisor_cds_d,
          ieor.aper07_assigned_to_cds_d,
          ieor.aper07_comment_x,
          ieor.aper07_last_updt_user_c,
          ieor.aper07_last_updt_s,
          ieor.apeh09_employee_k,
          ieor.aper06_req_status_k,
          ipw.apeh04_first_n,
          ipw.apeh04_cds_d,
          ipw.apeh04_last_n,
          ipw.apeh04_person_type_c,
          irs.aper06_req_status_x,
          par_val1.aper17_parameter_val_x,
          offr.aper15_offboard_reason_x
HAVING ipw.apeh04_person_type_c NOT IN( 'c', 'C' )
4

2 回答 2

4

你不能这样做。别名只能在当前别名的选择中使用。

您的问题有两种解决方案:

select a+b as newcol
from table
group by a+b;

或者

select newcol
from
   (select a+b as newcol
     from table)
group by newcol;

对于您的查询,我建议这样做:

select 
  reqno,
  ll6CdsID,
  reqtype,
  requeststatus,
  assignedTOiti,
  assignedTOasstAdm,
  assignedTOhlp,
  closedDate,
  aper07_assigned_to_cds_d,
  aper07_comment_x,
  aper07_last_updt_user_c,
  last,
  apeh09_employee_k,
  aper06_req_status_k,
  apeh04_first_n,
  apeh04_cds_d,
  apeh04_last_n,
  apeh04_person_type_c,
  aper06_req_status_x,
  offboardReason,
  Min(creationDate) AS creationDate
FROM

    (SELECT ieor.aper07_req_k                              AS reqno,
           ford.apeh09_supervisor_cds_d                   AS ll6CdsID,
           'Ford Offboard Req'                            AS reqtype,
           par_val1.aper17_parameter_val_x                AS requeststatus,
           (SELECT Min(iti2.aper07_req_created_s)
            FROM   iaper07_employee_offbrd_req iti2
            WHERE  ieor.aper07_req_k = iti2.aper07_req_k
                   AND iti2.aper06_req_status_k = 4)      AS assignedTOiti,
           (SELECT Min(hlp2.aper07_req_created_s)
            FROM   iaper07_employee_offbrd_req hlp2
            WHERE  ieor.aper07_req_k = hlp2.aper07_req_k
                   AND hlp2.aper06_req_status_k = 5)      AS assignedTOasstAdm,
           (SELECT Min(asst2.aper07_req_created_s)
            FROM   iaper07_employee_offbrd_req asst2
            WHERE  ieor.aper07_req_k = asst2.aper07_req_k
                   AND asst2.aper06_req_status_k = 12)    AS assignedTOhlp,
           (SELECT Min(clsd2.aper07_req_created_s)
            FROM   iaper07_employee_offbrd_req clsd2
            WHERE  ieor.aper07_req_k = clsd2.aper07_req_k
                   AND clsd2.aper06_req_status_k IN ( 3, 9 )) AS closedDate,
           ieor.aper07_assigned_to_cds_d,
           ieor.aper07_comment_x,
           ieor.aper07_last_updt_user_c,
           ieor.aper07_last_updt_s                            AS last,
           ieor.apeh09_employee_k,
           ieor.aper06_req_status_k,
           ipw.apeh04_first_n,
           ipw.apeh04_cds_d,
           ipw.apeh04_last_n,
           ipw.apeh04_person_type_c,
           irs.aper06_req_status_x,
           offr.aper15_offboard_reason_x                      AS offboardReason,
           aper07_req_created_s                          AS creationDate
    FROM   iaper07_employee_offbrd_req ieor,
           iapeh05_person_vw ipw,
           iapeh09_ford_employee ford,
           iaper06_req_status irs,
           iaper15_offbrd_req_reason offr,
           iaper17_parameter_values par_val1
    WHERE  ford.apeh09_cds_d = ipw.apeh04_cds_d
           AND ieor.apeh09_employee_k = ipw.apeh04_visitor_k
           AND irs.aper06_req_status_k = ieor.aper06_req_status_k
           AND offr.aper15_offboard_reason_k = ieor.aper15_offboard_reason_k
           AND ieor.aper06_req_status_k = par_val1.aper17_parameter_val_r
           AND ipw.apeh22_region_k = 1
           AND ieor.aper07_last_updt_s = (SELECT Max(aper07_last_updt_s)
                                          FROM   iaper07_employee_offbrd_req b
                                          WHERE  ieor.aper07_req_k = b.aper07_req_k)
           AND ieor.aper07_req_k = 3387
    )
WHERE apeh04_person_type_c NOT IN( 'c', 'C' )
GROUP  BY   reqno,
  ll6CdsID,
  reqtype,
  requeststatus,
  assignedTOiti,
  assignedTOasstAdm,
  assignedTOhlp,
  closedDate,
  aper07_assigned_to_cds_d,
  aper07_comment_x,
  aper07_last_updt_user_c,
  last,
  apeh09_employee_k,
  aper06_req_status_k,
  apeh04_first_n,
  apeh04_cds_d,
  apeh04_last_n,
  apeh04_person_type_c,
  aper06_req_status_x,
  offboardReason

我将have子句移到了WHERE子句中,尽管它可以放在内部select to WHERE子句中。

于 2012-10-11T06:35:01.813 回答
2

group by 子句在 SELECT 显示所选列之前对数据进行分组。在这个阶段,select 子句中的别名没有定义,因此您不能在 GROUP BY 中使用它们。

丑陋的解决方法是用正在使用的实际语句/函数替换别名。

于 2012-10-11T06:32:13.280 回答