2012-10-11 82 views
3

別名添加到組在下面的查詢如何添加別名assignedTOitiassignedTOasstAdmassignedTOhlpclosedDate到group by子句。如何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

你不能那樣做。別名只能在當前選擇中使用。

有兩種sollutions您的問題:

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 

我having子句移動到WHERE子句,儘管它可以放在WHERE子句的內部選擇中。

+0

你能幫助我與UR第二個建議...與關聯到我的query.SInce我的選擇子句在一組不允許由一個子查詢,而不是列。 。其中是什麼我不能做.. – ashwinsakthi

+0

謝謝你這麼much..Florin..but當我實現查詢我得到的一列的空值分配說,以幫助那裏的數據是實際存在的。 – ashwinsakthi

+0

採取內部查詢,刪除哪些是無關的'分配給help',看看什麼是錯的。可能是錯誤的條件,或錯誤的加入。 –

2

group by子句在SELECT顯示選定列之前對數據進行分組。在這個階段,select子句中的別名沒有定義,因此你不能在GROUP BY中使用它們。

醜陋的解決方法是用正在使用的實際語句/函數替換別名。

+0

它不工作...說子查詢的.. – ashwinsakthi