EEALL@ONCE

๐Ÿ“Š ์ฟผ๋ฆฌ ์Šคํ„ฐ๋””(1) - IFNULL / JOIN ์ „ ์กฐ๊ฑด / JOIN ํ›„ ์กฐ๊ฑด ๋ณธ๋ฌธ

Data๐Ÿ“Š/SQL ์ฟผ๋ฆฌ ์Šคํ„ฐ๋””

๐Ÿ“Š ์ฟผ๋ฆฌ ์Šคํ„ฐ๋””(1) - IFNULL / JOIN ์ „ ์กฐ๊ฑด / JOIN ํ›„ ์กฐ๊ฑด

์˜ฌ์—ฃ์›์Šค 2023. 7. 27. 17:58
728x90

๐Ÿ“Š ํ…Œ์ด๋ธ” : payment (์ˆ˜๋‚ฉ/ํ• ์ธ) / bill (์ฒญ๊ตฌ)

 

์ˆ˜๋‚ฉ/ํ• ์ธ
์ฒญ๊ตฌ

 

๐Ÿง๋ฌธ์ œ ์‚ฌํ•ญ: 

์ฒญ๊ตฌ ํ…Œ์ด๋ธ”๊ณผ ์ˆ˜๋‚ฉ ํ…Œ์ด๋ธ”์ด ์žˆ๋Š”๋ฐ, ์ฒญ๊ตฌ ํ…Œ์ด๋ธ” ํ•ญ๋ชฉ์— ๋Œ€ํ•œ ์ˆ˜๋‚ฉ์ด ๋˜์—ˆ๋Š”์ง€๋ฅผ ์ฒดํฌํ•˜๊ณ  ์‹ถ๋‹ค. 

 

๋‹ค๋งŒ ๋ชจ๋‘ ๋‚ฉ๋ถ€ ์ด์™ธ์—๋„ ๋ถ€๋ถ„์ ์œผ๋กœ ์ฐ”๋”์ฐ”๋” ๋‚ฉ๋ถ€ํ–ˆ์„ ๊ฐ€๋Šฅ์„ฑ๋„ ์žˆ๋Š” ์ƒํ™ฉ

๋˜ํ•œ ์ˆ˜๋‚ฉ ํ…Œ์ด๋ธ”์€ ๊ณ ๊ฐ์ด ์ˆ˜๋‚ฉํ•œ ๊ธˆ์•ก๋„ ์Œ“์ด์ง€๋งŒ ํšŒ์‚ฌ๊ฐ€ ํ• ์ธํ•ด์ฃผ๋Š” ๊ธˆ์•ก๋„ ์Œ“์ธ๋‹ค.

(์ฆ‰ ์ฒญ๊ตฌ๊ธˆ-์ˆ˜๋‚ฉ๊ธˆ-ํ• ์ธ๊ธˆ=0 ์ด ๋˜์–ด์•ผ ์™„๋‚ฉ์ด ๋œ๋‹ค.) 

 

๐Ÿ’ก์†”๋ฃจ์…˜:

SELECT bill.bill_sn, bill.ban, bill.bill_item, bill.charge_money, SUM(IFNULL(pym.pym_amt, 0)),
SUM(IFNULL(sale.pym_amt, 0)),
(bill.charge_money - SUM(IFNULL(pym.pym_amt, 0)) - SUM(IFNULL(sale.pym_amt, 0) ) as left_money
FROM bill 
LEFT JOIN payment AS pym ON 
    bill.bill_item = pym.pym_item 
    AND bill.ban = pym.ban
    AND pym.code = 'PYM'
LEFT JOIN payment AS sale ON
    bill.bill_item = sale.pym_item 
    AND bill.ban = sale.ban
    AND sale.code = 'SALE'
GROUP BY bill.bill_sn, bill.ban, bill.bill_item, bill.charge_money;


1. ์กฐํšŒ

`SELECT bill.bill_sn, bill.ban, bill.bill_item, bill.charge_money, SUM(IFNULL(pym.pym_amt, 0)), SUM(IFNULL(sale.pym_amt, 0)), (bill.charge_money - SUM(IFNULL(pym.pym_amt, 0)) - SUM(IFNULL(sale.pym_amt, 0)) as left_money`: ์กฐํšŒํ•˜๋ ค๋Š” ํ•„๋“œ๋ฅผ ๋ช…์‹œํ•ฉ๋‹ˆ๋‹ค. 

 ๐Ÿ”ด GROUP BY

GROUP BY ํ•ญ๋ชฉ์œผ๋กœ ์ ์ง€ ์•Š์€ ํ•ญ๋ชฉ์€ ์ง‘๊ณ„ ํ•จ์ˆ˜์™€ ํ•จ๊ป˜๊ฐ€ ์•„๋‹ˆ๋ฉด ๋‹จ๋…์œผ๋กœ ์“ธ ์ˆ˜ ์—†๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด, bill_date ๊ฐ™์€ ๊ฒฝ์šฐ GROUP BY๋กœ ๋ฌถ์—ฌ์žˆ์ง€ ์•Š๋‹ค. ๋”ฐ๋ผ์„œ SELECT ๋ฌธ ๋’ค์— ๋‹จ๋…์œผ๋กœ ์กฐํšŒํ•  ์ˆ˜ ์žˆ๋Š” ์ปฌ๋Ÿผ์œผ๋กœ ์“ธ ์ˆ˜ ์—†๋‹ค. 

์ง‘๊ณ„ํ•จ์ˆ˜ : max,count,sum,avg ๋“ฑ๋“ฑ...  

 ๐Ÿ”ด  IFNULL

`IFNULL`์€ ์ฒซ ๋ฒˆ์งธ ์ธ์ž๊ฐ€ `NULL`์ธ์ง€ ์•„๋‹Œ์ง€๋ฅผ ๊ฒ€์‚ฌํ•จ. ๋งŒ์•ฝ ์ฒซ ๋ฒˆ์งธ ์ธ์ž๊ฐ€ `NULL`์ด๋ฉด, ๋‘ ๋ฒˆ์งธ ์ธ์ž๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ , ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด ์ฒซ ๋ฒˆ์งธ ์ธ์ž๋ฅผ ๋ฐ˜ํ™˜.

SELECT IFNULL(name, 'No name') FROM users;


๋งŒ์•ฝ `name` ๊ฐ’์ด `NULL`์ด๋ผ๋ฉด, 'No name'์ด๋ผ๋Š” ๋ฌธ์ž์—ด์„ ๋Œ€์‹  ๋ฐ˜ํ™˜ํ•จ

 ๐Ÿ”ด  SUM ์ง‘๊ณ„ํ•จ์ˆ˜ 

(bill.charge_money - SUM(IFNULL(pym.pym_amt, 0)) - SUM(IFNULL(sale.pym_amt, 0)) as left_money

GROUP BY๋กœ ๋ฌถ์–ด ๋†“๊ณ , ๊ฐ ๊ฐ์˜ ๊ทธ๋ฃน์—์„œ ๊ณ ๊ฐ ๋‚ฉ๋ถ€ ๋ถ€๋ถ„๊ณผ  ํ• ์ธ ๋ถ€๋ถ„์˜ SUM์„ ๊ตฌํ•ด์„œ ํ•ด๋‹น ์ฒญ๊ตฌ ํ•ญ๋ชฉ์˜ ์ฒญ๊ตฌ ๊ธˆ์•ก์—์„œ ๋นผ์ฃผ๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค. 

์—ฌ๊ธฐ์„œ ์œ„์—์„œ ์„ค๋ช…ํ•œ IFNULL์„ ์‚ฌ์šฉํ•œ ์ด์œ ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. 

SELECT SUM(pym_amt) FROM payment WHERE code = 'PYM' ์ฟผ๋ฆฌ๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ๊ฒฐ๊ณผ๋Š” NULL์ด๋‹ค.  ์™œ๋ƒํ•˜๋ฉด NULL์€ '์•Œ ์ˆ˜ ์—†๋Š” ๊ฐ’'์„ ๋‚˜ํƒ€๋‚ด๊ธฐ ๋•Œ๋ฌธ์— ์–ด๋–ค ์ˆซ์ž์™€ ๋”ํ•˜๊ฑฐ๋‚˜ ๋นผ๋„ ๊ฒฐ๊ณผ๋Š” '์•Œ ์ˆ˜ ์—†๋Š” ๊ฐ’' ์ฆ‰, NULL์ด ๋˜๊ธฐ ๋•Œ๋ฌธ!! 

IFNULL ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ NULL ๊ฐ’์„ '0'์œผ๋กœ ๋Œ€์ฒดํ•˜๋ฉด, SELECT SUM(IFNULL(pym_amt, 0)) FROM payment WHERE code = 'PYM' ์ฟผ๋ฆฌ์˜ ๊ฒฐ๊ณผ๋Š” 100์ด๋จ ! 

๋งˆ์ฐฌ๊ฐ€์ง€๋กœ, ์šฐ๋ฆฌ๋Š” ์ง€๊ธˆ LEFT JOIN์„ ๋‘ ๋ฒˆ์ด๋‚˜ ํ•œ ์ƒํƒœ์ž„์œผ๋กœ!  ๋ฌด์ง€๋ง‰์ง€ํ•˜๊ฒŒ ๋งŽ์€ null ๊ฐ’์ด ๊ฐ ํ•ด๋‹น ๋ฐ์ดํ„ฐ์— ์žˆ์„ ๊ฐ€๋Šฅ์„ฑ์ด ๊ต‰์žฅํžˆ ๋†’๋‹ค!!

๋”ฐ๋ผ์„œ, ๋‚˜๋Š” ์—ฌ๊ธฐ์„œ IFNULL์„ ์‚ฌ์šฉํ•ด์„œ ์ด ๋ถ€๋ถ„์˜ ์˜ˆ์™ธ๋ฅผ ์ฒ˜๋ฆฌํ•ด์คฌ๋‹ค.

 

2. JOIN ์ „ ์กฐ๊ฑด / JOIN ํ›„ ์กฐ๊ฑด

`LEFT JOIN payment AS pym ON bill.bill_item = pym.pym_item AND bill.ban = pym.ban AND pym.code = 'PYM'`: 

`payment` ํ…Œ์ด๋ธ”์„ `bill` ํ…Œ์ด๋ธ”์— ๊ฒฐํ•ฉ

๊ฒฐํ•ฉ ์กฐ๊ฑด์€ `bill.bill_item`= `pym.pym_item`, `bill.ban`= `pym.ban` ๊ทธ๋ฆฌ๊ณ  `pym.code`๊ฐ€ 'PYM'์ธ ๊ฒฝ์šฐ

๋งŒ์•ฝ, pym.code = 'PYM'` ์ด ๋ถ€๋ถ„์ด ON ๋’ค์—๊ฐ€ ์•„๋‹ˆ๋ผ, WHERE ๋กœ ์ด์–ด์ง€๊ฒŒ ๋˜๋ฉด,

GPT์œ์— ๋”ฐ๋ฅด๋ฉด..  


`WHERE` ์ ˆ๊ณผ `JOIN` ์ ˆ์—์„œ์˜ ์กฐ๊ฑด์€ ๋‹ค๋ฅธ ๋ฐฉ์‹์œผ๋กœ ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค.

1. `JOIN` ์ ˆ์—์„œ ์กฐ๊ฑด์„ ์‚ฌ์šฉํ•˜๋ฉด, ๊ทธ ์กฐ๊ฑด์€ ๋‘ ํ…Œ์ด๋ธ”์ด ํ•ฉ์ณ์ง€๊ธฐ ์ „์— ์ ์šฉ๋ฉ๋‹ˆ๋‹ค. ์ฆ‰, ์—ฌ๊ธฐ์„œ `pym.code = 'PYM'`์€ `payment` ํ…Œ์ด๋ธ”์—์„œ `code`๊ฐ€ 'PYM'์ธ ํ–‰๋งŒ์„ ์„ ํƒํ•˜์—ฌ `bill` ํ…Œ์ด๋ธ”๊ณผ ๊ฒฐํ•ฉํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ `pym`์— ๊ด€๋ จ๋œ `SUM(IFNULL(pym.pym_amt, 0))` ๊ณ„์‚ฐ์€ `code`๊ฐ€ 'PYM'์ธ ํ–‰์—๋งŒ ์ ์šฉ๋ฉ๋‹ˆ๋‹ค.

2. `WHERE` ์ ˆ์—์„œ ๊ฐ™์€ ์กฐ๊ฑด์„ ์‚ฌ์šฉํ•˜๋ฉด, ๊ทธ ์กฐ๊ฑด์€ ๋ชจ๋“  ํ…Œ์ด๋ธ”์ด ํ•ฉ์ณ์ง„ ํ›„์— ์ ์šฉ๋ฉ๋‹ˆ๋‹ค. ์ฆ‰, `WHERE pym.code = 'PYM'` ์กฐ๊ฑด์€ `bill` ํ…Œ์ด๋ธ”๊ณผ `payment` ํ…Œ์ด๋ธ”์ด ๊ฒฐํ•ฉ๋œ ํ›„์— ์ ์šฉ๋ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ 'SALE'์— ํ•ด๋‹นํ•˜๋Š” ํ–‰๋“ค๋„ ํฌํ•จ๋˜์–ด `payment` ํ…Œ์ด๋ธ”์— 'PYM'๊ณผ 'SALE' ๋‘˜ ๋‹ค ์žˆ์„ ๋•Œ๋Š” ๊ฒฐ๊ณผ๊ฐ€ ๋‹ฌ๋ผ์ง‘๋‹ˆ๋‹ค.

์ฆ‰, ์ด ๋‘ ๊ฐ€์ง€ ์กฐ๊ฑด์€ ๋‹ค๋ฅธ ๊ฒฐ๊ณผ๋ฅผ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์–ด๋–ค ๊ฒฐ๊ณผ๋ฅผ ์›ํ•˜๋Š”์ง€์— ๋”ฐ๋ผ ์ ์ ˆํ•œ ์œ„์น˜์— ์กฐ๊ฑด์„ ๋ฐฐ์น˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.


 

์˜ˆ์‹œ : 

 




SELECT bill.bill_sn, bill.ban, bill.bill_item, bill.charge_money, SUM(IFNULL(pym.pym_amt, 0)), SUM(IFNULL(sale.pym_amt, 0)),
(bill.charge_money - SUM(IFNULL(pym.pym_amt, 0)) - SUM(IFNULL(sale.pym_amt, 0)) ) as left_money
FROM bill 
LEFT JOIN payment AS pym ON 
    bill.bill_item = pym.pym_item 
    AND bill.ban = pym.ban
    AND pym.code = 'PYM'
LEFT JOIN payment AS sale ON
    bill.bill_item = sale.pym_item 
    AND bill.ban = sale.ban
    AND sale.code = 'SALE'
GROUP BY bill.bill_sn, bill.ban, bill.bill_item, bill.charge_money;

 

SELECT bill.bill_sn, bill.ban, bill.bill_item, bill.charge_money, SUM(IFNULL(pym.pym_amt, 0)), SUM(IFNULL(sale.pym_amt, 0)),
(bill.charge_money - SUM(IFNULL(pym.pym_amt, 0)) - SUM(IFNULL(sale.pym_amt, 0)) ) as left_money
FROM bill 
LEFT JOIN payment AS pym ON 
    bill.bill_item = pym.pym_item 
    AND bill.ban = pym.ban
LEFT JOIN payment AS sale ON
    bill.bill_item = sale.pym_item 
    AND bill.ban = sale.ban
    AND sale.code = 'SALE'
WHERE pym.code = 'PYM'
GROUP BY bill.bill_sn, bill.ban, bill.bill_item, bill.charge_money;

๋”ฐ๋ผ์„œ,  ๋จธ๋ฆฌ ์†์—์„œ ํ…Œ์ด๋ธ” ํŒŒ์‹ฑ์„ ์ž˜ ํ•˜๋ฉด์„œ ์กฐ๊ฑด๋ฌธ์„ ๊ฑธ์–ด์ค˜์•ผํ•œ๋‹ค.

์•„๋‹ˆ๋ฉด, ์ด์ƒํ•œ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์™€์„œ ๊ธด ์ฟผ๋ฆฌ ๋‹ค ์“ฐ๊ณ  ์ง€์›Œ์•ผํ•  ๊ฒฝ์šฐ๊ฐ€ ์ƒ๊ฒจ๋ฒ„๋ฆฐ๋‹ค! 

728x90