»Ë»Ñ Æ÷·³
°³¹ßÀÚÆ÷·³ ÀÔ´Ï´Ù.
  • ºÏ¸¶Å© ¾ÆÀÌÄÜ

MSSQL WITH AS °ü·Ã Áú¹® µå¸³´Ï´Ù.

¾È³çÇϼ¼¿ä. ¼±¹è´Ôµé!

 

¸Ç³¯ ±Û¸¸ Àдٰ¡ óÀ½À¸·Î ±Û ³²°Üº¾´Ï´Ù.

 

À̹ø¿¡ DB°ü·Ã °øºÎ¸¦ ÇÏ´Ù°¡ with as ±¸¹®¿¡ °ü·ÃÇÏ¿© °øºÎÇÏ´Ù°¡ ´Ù¸¥ »çÀÌÆ®¿¡¼­ 

 

º» Å×À̺íÀ» º¸°í ±Ã±ÝÇÑÁ¡ÀÌ »ý°Ü¼­ Áú¹®µå¸³´Ï´Ù.

 

ÇöÀç tmp1ÀÇ Å×À̺íÀº ¾Æ·¡¿Í °°½À´Ï´Ù. 

tmp1            
cd pcd lv ÇÕ °¡ ³ª ´Ù
A NULL 1 NULL 0 0 0
A1 A 2 NULL 0 0 0
A1-1 A1 3 7 2 3 2
A1-2 A1 3 9 4 4 1
A2 A 2 2 2 0 0
B NULL 1 NULL 0 0 0
B1 B 2 6 5 0 1
B2 B 2 5 0 3 2


ÀÌ Å×À̺íÀ» °¡Áö°í 

 

with tmp2 as(

SELECT c.cd r_cd

 

     , c.cd, c.pcd, c.lv

     , c.ÇÕ, c.°¡, c.³ª, c.´Ù

  FROM tmp1 c

 UNION ALL

SELECT p.r_cd

     , c.cd, c.pcd, c.lv

     , c.ÇÕ, c.°¡, c.³ª, c.´Ù

  FROM tmp2 p

 INNER JOIN tmp1 c

    ON p.cd = c.pcd

)

 

¸¦ ½ÇÇàÇÏ°Ô µÇ¸é ¾Æ·¡¿Í °°Àº °á°ú °ªÀÌ »ý¼ºÀÌ µË´Ï´Ù.

 

tmp2              
r_cd cd pcd lv ÇÕ °¡ ³ª ´Ù
A A NULL 1 NULL 0 0 0
A1 A1 A 2 NULL 0 0 0
A1-1 A1-1 A1 3 7 2 3 2
A1-2 A1-2 A1 3 9 4 4 1
A2 A2 A 2 2 2 0 0
B B NULL 1 NULL 0 0 0
B1 B1 B 2 6 5 0 1
B2 B2 B 2 5 0 3 2
B B1 B 2 6 5 0 1
B B2 B 2 5 0 3 2
A1 A1-1 A1 3 7 2 3 2
A1 A1-2 A1 3 9 4 4 1
A A1 A 2 NULL 0 0 0
A A2 A 2 2 2 0 0
A A1-1 A1 3 7 2 3 2
A A1-2 A1 3 9 4 4 1

 

 

UNION ALL ºÎºÐÀ» ±âÁ¡À¸·Î tmp1¿¡ ÀÖ´Â °ªÀÌ ¿¨Ä¿ ¸â¹ö·Î Çؼ­ µé¾î°¡´Â°Ç ÀÌÇØ°¡ °¡´Âµ¥¿ä

 

tmp2 ¿Í tmp1ÀÌ inner join ÇÏ´Â ºÎºÐÀÌ ¸Ó¸®¼ÓÀ¸·Î ±×·ÁÁöÁö°¡ ¾Ê½À´Ï´Ù... Àç±ÍÀû È£Ãâ·Î ºÁ¾ßÇϴµ¥...

 

½Ç·ÂÀÌ ¸¹ÀÌ ºÎÁ·Çؼ­¿ä ¤Ð¤Ð µµÀúÈ÷ ¾î¶»°Ô »ý°¢ÇغÁ¾ßÇÒÁö ¸ô¶ó¼­¿ä.

 

 È¤½Ã ¾Æ½Ã´Â ¼±¹è´ÔµéÀÇ Á¶¾ðÀ» ±¸Çغ¸°í ½Í½À´Ï´Ù.

 

±ä±Û Àоî Áּż­ °¨»çÇÕ´Ï´Ù!!

 

(ps. °á°ú °ªÀ» ¿¢¼¿¿¡ ÀÖ´Â°É ºÙ¿© ³Ö±â Çß´õ´Ï ÀÌ»óÇÏ°Ô ±ÛÀÌ ±æ¾îÁö°Ô µÇ³×¿ä ¤Ð¤Ð)

0
ÃßõÇϱ⠴ٸ¥ÀÇ°ß 0
|
°øÀ¯¹öÆ°
  • ¾Ë¸² ¿å¼³, »óó ÁÙ ¼ö ÀÖ´Â ¾ÇÇÃÀº »ï°¡ÁÖ¼¼¿ä.
©¹æ »çÁø  
¡â ÀÌÀü±Û¡ä ´ÙÀ½±Û