3

以下のクエリでは、エイリアスassignedTOiti, assignedTOasstAdm,assignedTOhlpclosedDategroup 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

それをしてはいけない。エイリアスは、現在のエイリアスに対する選択でのみ使用できます。

あなたの問題には2つの解決策があります:

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

内部の select to WHERE 句に入れることはできますが、having 句を WHERE 句に移動しました。

于 2012-10-11T06:35:01.813 に答える
2

group by 句は、SELECT が選択された列を表示する前にデータをグループ化します。この段階では、select 句のエイリアスは定義されていないため、GROUP BY では使用できません。

醜い回避策は、エイリアスを実際に使用されているステートメント/関数に置き換えることです。

于 2012-10-11T06:32:13.280 に答える