๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

SQL

[SQL] HackerRank - SQL Project Planning ๋ฌธ์ œ

๋ฐ˜์‘ํ˜•

๐Ÿ”Š ๋ณธ ํฌ์ŠคํŒ…์—์„œ ์‚ฌ์šฉ๋˜๋Š” ํ…Œ์ด๋ธ”์˜ ์ž๋ฃŒ์™€ ์ถœ์ฒ˜๋Š” HackerRank ์ž„์„ ๋ฐํž™๋‹ˆ๋‹ค. ๋” ๋‹ค์–‘ํ•œ SQL ๋ฌธ์ œ๋ฅผ ํ’€์–ด๋ณด์‹œ๋ ค๋ฉด

HackerRank ์‚ฌ์ดํŠธ๋ฅผ ๋ฐฉ๋ฌธํ•ด ๋ณด์„ธ์š”!

 

HackerRank ์‚ฌ์ดํŠธ

 

์ด๋ฒˆ ํฌ์ŠคํŒ…์—์„œ๋Š” HackerRank์˜ SQL Project Planning ๋ฌธ์ œ๋ฅผ ํ’€์–ด๋ณด์ž. ์ด๋ฒˆ ๋ฌธ์ œ์—์„œ ์š”๊ตฌํ•˜๋Š” ์‚ฌํ•ญ์€ ํ•„์ž๊ฐ€ ์ง์ ‘ ์„ค๋ช…ํ•˜๊ธฐ ๋ณด๋‹ค๋Š” ๋ฌธ์ œ์˜ ์›๋ณธ์„ ์ฝ์–ด๋ณด๋Š” ๊ฒƒ์ด ๋” ์ˆ˜์›”ํ•  ๊ฒƒ์œผ๋กœ ํŒ๋‹จ๋œ๋‹ค. ๋ฌธ์ œ ์›๋ณธ์€ ์—ฌ๊ธฐ๋ฅผ ํด๋ฆญํ•ด ์‚ดํŽด๋ณด์ž.

 

ํ•ด๋‹น ๋ฌธ์ œ๋ฅผ ํ’€์–ด๋ณด๋ ค๋‹ค๊ฐ€ ๊ฒฐ๊ตญ ํ’€์ง€ ๋ชปํ•˜๊ณ  Discussion์„ ์‚ดํŽด๋ณด์•˜๋Š”๋ฐ, ๋งˆ์น˜ SQL๋ฌธ์ œ์ธ๋ฐ ๋ฐ์ดํ„ฐ ์†์—์„œ ํŒจํ„ด์„ ๋ฐœ๊ฒฌํ•ด์•ผ ํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ์ฒ˜๋Ÿผ ๋Š๊ปด์กŒ๋‹ค.. ๋ฌธ์ œ๋ฅผ ํ’€๊ธฐ ์œ„ํ•œ ์•„์ด๋””์–ด๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

 

  • Start_Date ์ค‘ End_Date์—๋Š” ์—†๋Š” Start_Date๋Š” ๊ฐ ์—ฐ์†์ ์ด์ง€ ์•Š๊ณ  ๊ฐœ๋ณ„์ ์œผ๋กœ ์‹œ์ž‘ํ•˜๋Š” ํ”„๋กœ์ ํŠธ์˜ ์‹œ์ž‘๋‚ ์งœ๋ฅผ ์˜๋ฏธํ•œ๋‹ค.
  • End_Date ์ค‘ Start_Date์—๋Š” ์—†๋Š” End_Date๋Š” ๊ฐ ์—ฐ์†์ ์ด์ง€ ์•Š๊ณ  ๊ฐœ๋ณ„์ ์œผ๋กœ ์‹œ์ž‘ํ•˜๋Š” ํ”„๋กœ์ ํŠธ์˜ ์ข…๋ฃŒ๋‚ ์งœ๋ฅผ ์˜๋ฏธํ•œ๋‹ค.

๋”ฐ๋ผ์„œ ์œ„ 2๊ฐ€์ง€ ์•„์ด๋””์–ด๋ฅผ ๋ฐ˜์˜ํ•œ ์ฟผ๋ฆฌ๋ฌธ์„ ์ž‘์„ฑํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. ์ฟผ๋ฆฌํ™”๋ฉด์„ ์•Œ์•„๋ณด๊ธฐ ์‰ฝ๊ฒŒ Start_Date๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•œ ํ›„ ์‚ดํŽด๋ณด์•˜๋‹ค.

 

SELECT Start_Date, End_Date
FROM 
    (SELECT Start_Date 
     FROM Projects 
     WHERE Start_Date NOT IN (SELECT End_Date FROM Projects)) a,
    (SELECT End_Date
     FROM Projects
    WHERE End_Date NOT IN (SELECT Start_Date FROM Projects)) b
 ORDER BY Start_Date

 

๊ฒฐ๊ณผ ํ™”๋ฉด

 

์œ„ ์ฒ˜๋Ÿผ Start_Date์™€ End_Date๊ฐ€ ์„œ๋กœ JOIN ๋œ ๊ฒƒ์ฒ˜๋Ÿผ ๋‚˜์˜ค๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ์—ฌ๊ธฐ์„œ ์‹œ์ž‘๋‚ ์งœ๊ฐ€ ์ข…๋ฃŒ๋‚ ์งœ๋ณด๋‹ค ์•ž์„œ์•ผ ๋œ๋‹ค๋Š” ์กฐ๊ฑด์„ ์ถ”๊ฐ€ํ•ด์ฃผ์–ด์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ฟผ๋ฆฌ๋ฌธ์„ ์ž‘์„ฑํ•œ๋‹ค.

 

SELECT Start_Date, End_Date
FROM 
    (SELECT Start_Date 
     FROM Projects 
     WHERE Start_Date NOT IN (SELECT End_Date FROM Projects)) a,
    (SELECT End_Date
     FROM Projects
    WHERE End_Date NOT IN (SELECT Start_Date FROM Projects)) b
WHERE Start_Date < End_Date

 

๋งˆ์ง€๋ง‰์œผ๋กœ ์‹œ์ž‘๋‚ ์งœ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๊ทธ๋ฃนํ•‘ํ•˜๋Š”๋ฐ, ์ด ๋•Œ ์ข…๋ฃŒ๋‚ ์งœ์˜ ์ตœ์†Ÿ๊ฐ’์„ ์ถœ๋ ฅํ•ด์ฃผ์–ด์•ผ ์šฐ๋ฆฌ๊ฐ€ ์›ํ•˜๋Š” ๊ฒฐ๊ณผ๊ฐ’์ด ๋‚˜์˜ค๊ฒŒ ๋œ๋‹ค.

 

SELECT Start_Date, MIN(End_Date)
FROM 
    (SELECT Start_Date 
     FROM Projects 
     WHERE Start_Date NOT IN (SELECT End_Date FROM Projects)) a,
    (SELECT End_Date
     FROM Projects
    WHERE End_Date NOT IN (SELECT Start_Date FROM Projects)) b
WHERE Start_Date < End_Date
GROUP BY Start_Date
ORDER BY DATEDIFF(MIN(End_Date), Start_Date), Start_Date

 

๊ทธ๋Ÿฐ๋ฐ ์—ฌ๊ธฐ์„œ ํ•œ ๊ฐ€์ง€ ์ƒˆ๋กœ์šด ์‚ฌ์‹ค์„ ์–ป๊ฒŒ ๋˜์—ˆ๋‹ค. MySQL 5.7๋ฒ„์ „ ์ด์ƒ๋ถ€ํ„ฐ ์ถ”๊ฐ€๋œ sql_mode์˜ only_full_group_by๋ชจ๋“œ๋ฅผ ํ•ด์ œํ•ด์ฃผ๋ฉด ์‹œ์ž‘๋‚ ์งœ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๊ทธ๋ฃนํ•‘ํ–ˆ์Œ์—๋„ ๋ถˆ๊ตฌํ•˜๊ณ  ์ข…๋ฃŒ๋‚ ์งœ๋ฅผ ์ตœ์†Ÿ๊ฐ’์ด ์•„๋‹Œ ๊ทธ๋ƒฅ End_Date๋กœ ๋ช…์‹œํ•ด์ฃผ์–ด๋„ ์›ํ•˜๋Š” ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•ด์ค„ ์ˆ˜ ์žˆ๋‹ค.

 

-- sql_mode ๋ณ€๊ฒฝ ๋ฐฉ๋ฒ•
SET sql_mode ='';
SELECT Start_Date, End_Date
FROM 
    (SELECT Start_Date 
     FROM Projects 
     WHERE Start_Date NOT IN (SELECT End_Date FROM Projects)) a,
    (SELECT End_Date
     FROM Projects
    WHERE End_Date NOT IN (SELECT Start_Date FROM Projects)) b
WHERE Start_Date < End_Date
GROUP BY Start_Date
ORDER BY DATEDIFF(End_Date, Start_Date), Start_Date
๋ฐ˜์‘ํ˜•