๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿง‘‍๐Ÿ’ผ SAP/ABAP

[SAP ABAP] JOIN๊ณผ FOR ALL ENTRIES ์ฐจ์ด

by Jay Din 2024. 7. 10.
728x90
๋ฐ˜์‘ํ˜•

์ฐจ์ด์  ์ •๋ฆฌ

ํŠน์ง• JOIN FOR ALL ENTRIES
์ˆ˜ํ–‰ ์œ„์น˜ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์„œ๋ฒ„ ABAP ์„œ๋ฒ„
์‚ฌ์šฉ ์šฉ๋„ ์—ฌ๋Ÿฌ ํ…Œ์ด๋ธ” ๊ฐ„์˜ ๊ด€๊ณ„๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฒฐํ•ฉํ•˜์—ฌ ์กฐํšŒ ์ธํ„ฐ๋„ ํ…Œ์ด๋ธ”์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ์กฐ๊ฑด์— ๋งž๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์กฐํšŒ
์„ฑ๋Šฅ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๋ ˆ๋ฒจ์—์„œ ์ตœ์ ํ™”๋˜๋ฏ€๋กœ, ์ž˜ ์„ค๊ณ„๋œ ์ธ๋ฑ์Šค์™€ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜๋ฉด ๊ณ ์„ฑ๋Šฅ์„ ๋ฐœํœ˜ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ ‘๊ทผ ํšŸ์ˆ˜๋ฅผ ์ค„์ผ ์ˆ˜ ์žˆ์œผ๋‚˜, ์ธํ„ฐ๋„ ํ…Œ์ด๋ธ”์ด ํฌ๋ฉด ์„ฑ๋Šฅ์ด ์ €ํ•˜๋  ์ˆ˜ ์žˆ์Œ
๋ณต์žกํ•œ ์กฐ๊ฑด ์ฒ˜๋ฆฌ ์—ฌ๋Ÿฌ ํ…Œ์ด๋ธ” ๊ฐ„์˜ ๋ณต์žกํ•œ ๊ด€๊ณ„์™€ ์กฐ๊ฑด์„ ์ฒ˜๋ฆฌ ๊ฐ€๋Šฅ ๋ณต์žกํ•œ ์กฐ์ธ๋ณด๋‹ค๋Š” ์กฐ๊ฑด ๊ธฐ๋ฐ˜ ์กฐํšŒ์— ์ ํ•ฉ
์ฃผ์˜์‚ฌํ•ญ ์กฐ์ธ์˜ ์ข…๋ฅ˜์— ๋”ฐ๋ผ ๊ฒฐ๊ณผ๊ฐ€ ๋‹ฌ๋ผ์งˆ ์ˆ˜ ์žˆ์Œ
(INNER JOIN, LEFT JOIN ๋“ฑ)
์ธํ„ฐ๋„ ํ…Œ์ด๋ธ”์ด ๋น„์–ด ์žˆ์œผ๋ฉด ์ „์ฒด ๋ฐ์ดํ„ฐ๊ฐ€ ์กฐํšŒ๋  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ, ์ธํ„ฐ๋„ ํ…Œ์ด๋ธ”์ด ๋น„์–ด ์žˆ์ง€ ์•Š์€์ง€ ํ™•์ธ ํ•„์š”
์˜ˆ์ œ INNER JOIN, LEFT JOIN ๋“ฑ์„ ์‚ฌ์šฉํ•˜์—ฌ ์—ฌ๋Ÿฌ ํ…Œ์ด๋ธ”์„ ๊ฒฐํ•ฉ ์ธํ„ฐ๋„ ํ…Œ์ด๋ธ”์„ ๊ธฐ๋ฐ˜์œผ๋กœ FOR ALL ENTRIES ๊ตฌ๋ฌธ ์‚ฌ
"๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๋ ˆ๋ฒจ"์—์„œ ์ˆ˜ํ–‰๋˜๋Š” ํŠน์ • ์ž‘์—…์€ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์„œ๋ฒ„์—์„œ ์ง์ ‘ ์ฒ˜๋ฆฌ๋˜๋Š” ์ž‘์—…์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.
์ด๋Ÿฌํ•œ ์ž‘์—…๋“ค์€ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์—”์ง„์ด ์ตœ์ ํ™”๋œ ๋ฐฉ์‹์œผ๋กœ ์ˆ˜ํ–‰๋˜๋ฉฐ, ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ ์„ฑ๋Šฅ์„ ๊ทน๋Œ€ํ™”ํ•˜๋Š” ๋ฐ ๋„์›€์„ ์ค๋‹ˆ๋‹ค.
๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๋ ˆ๋ฒจ์—์„œ ์ˆ˜ํ–‰๋˜๋Š” ๋ช‡ ๊ฐ€์ง€ ํŠน์ • ์ž‘์—… ์˜ˆ์‹œ๋กœ๋Š” JOIN, ์ง‘๊ณ„์ž‘์—…(COUNT) ๋“ฑ์ด ์žˆ์Šต๋‹ˆ๋‹ค.

 

JOIN ์ด๋ž€?

JOIN์€ ๋‘ ๊ฐœ ์ด์ƒ์˜ ํ…Œ์ด๋ธ”์„ ๊ฒฐํ•ฉํ•˜์—ฌ ๋‹จ์ผ ๊ฒฐ๊ณผ ์„ธํŠธ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” SQL ๊ตฌ๋ฌธ์ž…๋‹ˆ๋‹ค.

๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๋ ˆ๋ฒจ์—์„œ ์ˆ˜ํ–‰๋˜๋ฉฐ, ํ…Œ์ด๋ธ” ๊ฐ„์˜ ๊ด€๊ณ„๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์กฐํšŒํ•ฉ๋‹ˆ๋‹ค.

 

ํŠน์ง•

  1. ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๋ ˆ๋ฒจ์—์„œ ์ˆ˜ํ–‰:
    • ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์„œ๋ฒ„์—์„œ JOIN ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•˜๋ฏ€๋กœ ์„œ๋ฒ„์˜ ์ž์›์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
  2. ๋ณต์žกํ•œ ์กฐ๊ฑด ์ฒ˜๋ฆฌ ๊ธฐ๋Šฅ:
    • ์—ฌ๋Ÿฌ ํ…Œ์ด๋ธ” ๊ฐ„์˜ ๊ด€๊ณ„๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ๋ณต์žกํ•œ ์กฐ๊ฑด์„ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  3. ๊ณ ์„ฑ๋Šฅ:
    • ์ž˜ ์„ค๊ณ„๋œ ์ธ๋ฑ์Šค๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ ๊ณ ์„ฑ๋Šฅ์„ ๋ฐœํœ˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  4. ์œ ํ˜•:
    • INNER JOIN, LEFT JOIN, RIGHT JOIN, FULL JOIN ๋“ฑ์ด ์žˆ์Šต๋‹ˆ๋‹ค.

 

์˜ˆ์ œ

๋‹ค์Œ์€ SFLIGHT์™€ SCAARR ํ…Œ์ด๋ธ”์„ INNER JOIN์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ฒฐํ•ฉํ•˜๋Š” ์˜ˆ์ œ์ž…๋‹ˆ๋‹ค.

* ๊ตฌ์กฐ์ฒด ํƒ€์ž… ์ •์˜
TYPES: BEGIN OF TY_RESULT,
         CARRID   TYPE SFLIGHT-CARRID,
         CONNID   TYPE SFLIGHT-CONNID,
         CARRNAME TYPE SCARR-CARRNAME,
       END OF TY_RESULT.

* ์ธํ„ฐ๋„ ํ…Œ์ด๋ธ” ์„ ์–ธ
DATA: LT_RESULTS TYPE TABLE OF TY_RESULT,
      WA_RESULTS TYPE TY_RESULT.

" SFLIGHT์™€ SCARR ํ…Œ์ด๋ธ”์„ ์กฐ์ธํ•˜์—ฌ ๋ฐ์ดํ„ฐ ์„ ํƒ
SELECT A~CARRID A~CONNID B~CARRNAME
  INTO TABLE LT_RESULTS
  FROM SFLIGHT AS A
  INNER JOIN SCARR AS B ON A~CARRID = B~CARRID
  WHERE A~CARRID = 'LH'.

" ์„ ํƒ๋œ ๋ฐ์ดํ„ฐ๋ฅผ ์ถœ๋ ฅ
LOOP AT LT_RESULTS INTO WA_RESULTS.
  WRITE: / WA_RESULTS-CARRID, WA_RESULTS-CONNID, WA_RESULTS-CARRNAME.
ENDLOOP.
  • SFLIGHT ํ…Œ์ด๋ธ”์˜ carrid์™€ scarr ํ…Œ์ด๋ธ”์˜ carrid๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๊ฒฐํ•ฉํ•ฉ๋‹ˆ๋‹ค.
  • ํŠน์ • ํ•ญ๊ณต์‚ฌ ID('LH')์— ํ•ด๋‹นํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์กฐํšŒํ•ฉ๋‹ˆ๋‹ค.

์‹คํ–‰ ๊ฒฐ๊ณผ

 

FOR ALL ENTRIES ๋ž€?

FOR ALL ENTREIS ๋Š” ์ธํ„ฐ๋„ ํ…Œ์ด๋ธ”์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ SQL SELECT ๋ฌธ์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค.

ํŠน์ • ์กฐ๊ฑด์— ๋งž๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์กฐํšŒํ•  ๋•Œ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.

 

ํŠน์ง•

  • ์ธํ„ฐ๋„ ํ…Œ์ด๋ธ” ๊ธฐ๋ฐ˜:
    • ABAP ์ธํ„ฐ๋„ ํ…Œ์ด๋ธ”์„ ๊ธฐ๋ฐ˜์œผ๋กœ SQL SELECT ๋ฌธ์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.
  • ์กฐ๊ฑด ๋งŒ์กฑ ๋ฐ์ดํ„ฐ ์กฐํšŒ:
    • ์ธํ„ฐ๋„ ํ…Œ์ด๋ธ”์˜ ๊ฐ ํ–‰์— ๋Œ€ํ•ด ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์กฐํšŒํ•ฉ๋‹ˆ๋‹ค.
  • ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ ‘๊ทผ ์ตœ์†Œํ™”:
    • ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋‹จ์ผ ์กฐ๊ฑด ์กฐํšŒ๋ฅผ ํ•˜๋‚˜์˜ ์ฟผ๋ฆฌ๋กœ ํ†ตํ•ฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ ‘๊ทผ ํšŸ์ˆ˜๋ฅผ ์ค„์ž…๋‹ˆ๋‹ค.
  • ์ฃผ์˜์‚ฌํ•ญ:
    • ์ธํ„ฐ๋„ ํ…Œ์ด๋ธ”์ด ๋น„์–ด ์žˆ์œผ๋ฉด ์ „์ฒด ๋ฐ์ดํ„ฐ๊ฐ€ ์กฐํšŒ๋  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ, ์ธํ„ฐ๋„ ํ…Œ์ด๋ธ”์ด ๋น„์–ด ์žˆ์ง€ ์•Š์€์ง€ ํ™•์ธํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

 

์˜ˆ์ œ

๋‹ค์Œ์€ SFLIGHT ํ…Œ์ด๋ธ”์—์„œ ํ•ญ๊ณต์‚ฌ ID๊ฐ€ LH์ธ ํ•ญ๊ณตํŽธ ๋ฐ์ดํ„ฐ๋ฅผ FOR ALL ENTRIES๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์กฐํšŒํ•˜๋Š” ์˜ˆ์ œ์ž…๋‹ˆ๋‹ค.

* ์ธํ„ฐ๋„ ํ…Œ์ด๋ธ” ๋ฐ ์ž‘์—… ์˜์—ญ ์„ ์–ธ
TYPES: BEGIN OF ty_carrid,
         carrid TYPE sflight-carrid,
       END OF ty_carrid.

DATA: lt_carrid  TYPE TABLE OF ty_carrid,
      lt_sflight TYPE TABLE OF sflight,
      wa_sflight TYPE sflight.

  " SFLIGHT ํ…Œ์ด๋ธ”์—์„œ carrid๊ฐ€ 'LH'์ธ ํ•ญ๊ณต์‚ฌ ID๋ฅผ ์„ ํƒ
  SELECT DISTINCT carrid
    INTO TABLE lt_carrid
    FROM sflight
    WHERE carrid = 'LH'.

  " ์ธํ„ฐ๋„ ํ…Œ์ด๋ธ”์ด ๋น„์–ด ์žˆ์ง€ ์•Š์€ ๊ฒฝ์šฐ์—๋งŒ FOR ALL ENTRIES ์‚ฌ์šฉ
  IF lt_carrid IS NOT INITIAL.
    SELECT *
      INTO TABLE lt_sflight
      FROM sflight
      FOR ALL ENTRIES IN lt_carrid
      WHERE carrid = lt_carrid-carrid.
  ELSE.
    WRITE: / 'No data found in lt_carrid'.
  ENDIF.

  " ์„ ํƒ๋œ ๋ฐ์ดํ„ฐ๋ฅผ ์ถœ๋ ฅ
  IF lt_sflight IS NOT INITIAL.
    LOOP AT lt_sflight INTO wa_sflight.
      WRITE: / wa_sflight-carrid, wa_sflight-connid, wa_sflight-fldate.
    ENDLOOP.
  ELSE.
    WRITE: / 'No data found in lt_sflight'.
  ENDIF.
  • lt_carrid ์ธํ„ฐ๋„ ํ…Œ์ด๋ธ”์„ ๊ธฐ๋ฐ˜์œผ๋กœ SFLIGHT ํ…Œ์ด๋ธ”์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์กฐํšŒํ•ฉ๋‹ˆ๋‹ค.
  • ์ธํ„ฐ๋„ ํ…Œ์ด๋ธ”์ด ๋น„์–ด ์žˆ์ง€ ์•Š์€ ๊ฒฝ์šฐ์—๋งŒ ์กฐํšŒ๋ฅผ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.

์‹คํ–‰ ํ™”๋ฉด

 

์ •๋ฆฌํ•˜๋ฉด,

  • JOIN: ์—ฌ๋Ÿฌ ํ…Œ์ด๋ธ” ๊ฐ„์˜ ๋ณต์žกํ•œ ๊ด€๊ณ„๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฒฐํ•ฉํ•˜์—ฌ ์กฐํšŒํ•  ๋•Œ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๋ ˆ๋ฒจ์—์„œ ์ตœ์ ํ™”๋˜๋ฏ€๋กœ ์„ฑ๋Šฅ์ด ์šฐ์ˆ˜ํ•ฉ๋‹ˆ๋‹ค.
  • FOR ALL ENTRIES: ์ธํ„ฐ๋„ ํ…Œ์ด๋ธ”์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ์กฐ๊ฑด์— ๋งž๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์กฐํšŒํ•  ๋•Œ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ ‘๊ทผ ํšŸ์ˆ˜๋ฅผ ์ค„์ผ ์ˆ˜ ์žˆ์ง€๋งŒ, ์ธํ„ฐ๋„ ํ…Œ์ด๋ธ”์ด ๋น„์–ด ์žˆ์ง€ ์•Š์€์ง€ ํ™•์ธํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค.

 

728x90
๋ฐ˜์‘ํ˜•