Banner

My Tech Blog (SQL)

๐Ÿ“‘ 1. ๋ฌธ์ œ์„ค๋ช…๐Ÿ˜‰ 2. ๋ฌธ์ œ ์š”์•ฝ๋ฌธ์ œ์—์„œ ์ฃผ์–ด์ง„ ์กฐ๊ฑดPATIENTDOCTORAPPOINTMENTํ™˜์ž ์ •๋ณด์˜์‚ฌ ์ •๋ณด์ง„๋ฃŒ ์˜ˆ์•ฝ ๋ชฉ๋กPT_NO, PT_NAME, GEND_CD, AGE, TLNODR_NAME, DR_ID, LCNS_NO, HIRE_YMD, MCDP_CD, TLNOAPNT_YMD, APNT_NO, PT_NO, MCDP_CD, MDDR_ID, APNT_CNCL_YN, APNT_CNCL_YMDํ™˜์ž๋ฒˆํ˜ธ, ํ™˜์ž์ด๋ฆ„, ์„ฑ๋ณ„์ฝ”๋“œ, ๋‚˜์ด, ์ „ํ™”๋ฒˆํ˜ธ์˜์‚ฌ์ด๋ฆ„, ์˜์‚ฌID, ๋ฉดํ—ˆ๋ฒˆํ˜ธ, ๊ณ ์šฉ์ผ์ž, ์ง„๋ฃŒ๊ณผ์ฝ”๋“œ, ์ „ํ™”๋ฒˆํ˜ธ์ง„๋ฃŒ ์˜ˆ์•ฝ์ผ์‹œ, ์ง„๋ฃŒ์˜ˆ์•ฝ๋ฒˆํ˜ธ, ํ™˜์ž๋ฒˆํ˜ธ, ์ง„๋ฃŒ๊ณผ์ฝ”๋“œ, ์˜์‚ฌID, ์˜ˆ์•ฝ์ทจ์†Œ์—ฌ๋ถ€, ์˜ˆ์•ฝ์ทจ์†Œ๋‚ ์งœ ๋ฌธ์ œ์ชผ๊ฐœ๊ธฐโœ… 2022๋…„ 4์›” 13์ผ AP.APNT_YMD LIKE '2022-04-13%'โœ… ์ทจ์†Œ๋˜์ง€ ์•Š์€..
๐Ÿ“‘ 1. ๋ฌธ์ œ์„ค๋ช…โœ๏ธ 2. ๋ฌธ์ œ ์š”์•ฝ๋ฌธ์ œ์—์„œ ์ฃผ์–ด์ง„ ์กฐ๊ฑดCAR_RENTAL_COMPANY_CAR CAR_RENTAL_COMPANY_RENTAL_HISTORY CAR_RENTAL_COMPANY_DISCOUNT_PLAN๋Œ€์—ฌ ์ค‘์ธ ์ž๋™์ฐจ๋“ค์˜ ์ •๋ณด์ž๋™์ฐจ ๋Œ€์—ฌ ๊ธฐ๋ก ์ •๋ณด์ž๋™์ฐจ ์ข…๋ฅ˜ ๋ณ„ ๋Œ€์—ฌ ๊ธฐ๊ฐ„ ์ข…๋ฅ˜ ๋ณ„ ํ• ์ธ ์ •์ฑ… ์ •๋ณดCAR_ID, CAR_TYPE, DAILY_FEE, OPTIONSHISTORY_ID, CAR_ID, START_DATE, END_DATEPLAN_ID, CAR_TYPE, DURATION_TYPE, DISCOUNT_RATE- ์ž๋™์ฐจ ID,- ์ž๋™์ฐจ ์ข…๋ฅ˜,- ์ผ์ผ ๋Œ€์—ฌ ์š”๊ธˆ(์›),- ์ž๋™์ฐจ ์˜ต์…˜ ๋ฆฌ์ŠคํŠธ,- ์ž๋™์ฐจ ๋Œ€์—ฌ ๊ธฐ๋ก ID,- ์ž๋™์ฐจ ID- ๋Œ€์—ฌ ์‹œ์ž‘์ผ, - ๋Œ€์—ฌ ์ข…๋ฃŒ์ผ- ์š”๊ธˆ ํ• ์ธ ์ •์ฑ… ID,..
๐Ÿ“‘ 1. ๋ฌธ์ œ์„ค๋ช…โญ 2. ์ •๋‹ต์ฝ”๋“œ๋‚ด๊ฐ€ ํ‘ผ ์ฝ”๋“œ ORDER BY DATEDIFF (์ž…์†Œ์ผ, ํ‡ด์†Œ์ผ)SELECT I.ANIMAL_ID, I.NAMEFROM ANIMAL_INS I JOIN ANIMAL_OUTS O ON I.Animal_id = O.Animal_idORDER BY DATEDIFF(I.DATETIME, O.DATETIME)LIMIT 2์ด๋ ‡๊ฒŒ ํ•ด์„œ ์ •๋‹ต์ฒ˜๋ฆฌ๊ฐ€ ๋ฌ๋Š”๋ฐ ๋‹ค๋ฅธ ์‚ฌ๋žŒ๋“ค์ด ์“ด ์ฝ”๋“œ๋ฅผ ๋ณด๋‹ค๊ฐ€ ๋ญ”๊ฐ€ ์ด์ƒํ•œ ์  ๋ฐœ๊ฒฌ!๋ณดํ˜ธ์†Œ ํ‡ด์†Œ์ผ - ์ž…์†Œ์ผ ์„ ํ•ด์„œ ๊ทธ ๊ฐ’์ด ํฐ ์ˆœ์„œ๋Œ€๋กœ 2๊ฑด์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฑด๋ฐ๋‚˜๋Š” ์ž…์†Œ์ผ - ํ‡ด์†Œ์ผ๋กœ ๋ฐ˜๋Œ€๋กœ ์ ์—ˆ๋‹ค ๋Œ€์‹  ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ํ•˜๋‹ˆ๊นŒ ์ž‘๋™ํ•œ๋‹ค.๐Ÿฆ 3. ๋‹ค๋ฅธ ์‚ฌ๋žŒ๋“ค์ด ํ‘ผ ์ฝ”๋“œORDER BY DATEDIFF (ํ‡ด์†Œ์ผ, ์ž…์†Œ์ผ) DESCSELECT I.ANIMAL_ID, I.N..
๐Ÿ“‘ 1. ๋ฌธ์ œ์„ค๋ช…โŒ 2. ์‹คํŒจํ•œ ์‹œ๋„์œ„์น˜ํ‹€๋ฆฐ๋ถ€๋ถ„๋งž๋Š” ์ฟผ๋ฆฌ์„ค๋ช…SELECTAVERAGEAVG()ํ‰๊ท ๊ตฌํ•˜๋Š” ํ•จ์ˆ˜AVERAGE()๊ฐ€ ์•„๋‹ˆ๊ณ AVG()์ž„ YEAR(YM)YEAR(YM) AS `YEAR`๋ณ„์นญ ์จ์•ผ ํ•จ์ปฌ๋Ÿผ๋ช… YEAR๋กœ ์ถœ๋ ฅ ROUND(AVG(PM_VAL1),3) ROUND(AVG(PM_VAL1),2)์†Œ์ˆ˜์…‹์งธ์ž๋ฆฌ์—์„œ ๋ฐ˜์˜ฌ๋ฆผํ•˜๋ ค๋ฉด ๋‘˜์งธ์ž๋ฆฌ๊นŒ์ง€ ๊ฒฐ๊ณผ๊ฐ’์ด ๋‚˜ํƒ€๋‚˜์•ผ ํ•˜๋‹ˆ๊นŒROUND(์ปฌ๋Ÿผ๋ช…, 2)๋กœ ํ•ด์•ผ ํ•จWHERELocation2 IS '์ˆ˜์›'Location2 = '์ˆ˜์›'IS๋Š” NULL ๊ฐ’๊ณผ์˜ ๋น„๊ต์—์„œ ๋งŒ ์‚ฌ์šฉ๋จORDER BYYEAR(YM)YEARSQL์˜ ์‹คํ–‰์ˆœ์„œ๋Š”ORDER BY์ ˆ์ด ๊ฐ€์žฅ๋งˆ์ง€๋ง‰์— ์‹คํ–‰๋˜๊ธฐ ๋•Œ๋ฌธ์—ALIAS ๋ช…์œผ๋กœ ์จ์ค˜๋„ ๋œ๋‹ค๊ผญ ๋ณ„์นญ ์จ์•ผํ•˜๋Š” ๊ฑด ์•„๋‹˜ SELECT YEAR(YM) AS YEAR,..
๐Ÿ“‘ 1. ๋ฌธ์ œ์„ค๋ช…โŒ 2. ์‹คํŒจํ•œ ์ฝ”๋“œ PRODUCT_CODE ์ปฌ๋Ÿผ์ด ์˜ˆ๋ฅผ ๋“ค๋ฉด 'A1000011' ์ด๊ธฐ ๋•Œ๋ฌธ์—SUBSTRING(์ปฌ๋Ÿผ๋ช…,์‹œ์ž‘์ธ๋ฑ์Šค,๋์ธ๋ฑ์Šค)๋กœ ์•ž ๋‘ ์ž๋ฆฌ๋งŒ ๋–ผ์–ด ๋‚ด์•ผ ํ•œ๋‹ค. SELECT SUBSTRING(Product_code,1,2) AS CATEGORY, COUNT(SUBSTRING(Product_code,1,2)) AS PRODUCTSFROM PRODUCTGROUP BY SUBSTRING(Product_code,1,2), Product_codeORDER BY Category; ๋‚ด๊ฐ€ ์ž‘์„ฑํ•œ ์ฝ”๋“œ์˜ ์‹คํ–‰ ๊ฒฐ๊ณผ๋ฅผ ๋ณด๋ฉด A2 ๊ธฐ์ค€์œผ๋กœ GROUP ์œผ๋กœ ๋ฌถ์ด์ง€ ์•Š์€ ๊ฒƒ์„ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ๋‹ค.โญ 3. ์ •๋‹ต์ฝ”๋“œGROUP BY ์ ˆ์—์„œ SUBSTRING(Product_code,1,2)๋กœ๋งŒ ๋ฌถ์–ด์•ผ ํ•จP..
๐Ÿ“‘ 1. ๋ฌธ์ œ์„ค๋ช…โŒ 2. ์‹คํŒจํ•œ ์‹œ๋„SELECT CASE WHEN SUBSTRING(DIFFERENTIATION_DATE, 6,7) IN ('01', '02', '03') THEN '1Q' WHEN SUBSTRING(DIFFERENTIATION_DATE, 6,7) IN ('04', '05', '06') THEN '2Q' WHEN SUBSTRING(DIFFERENTIATION_DATE, 6,7) IN ('07', '08', '09') THEN '3Q' WHEN SUBSTRING(DIFFERENTIATION_DATE, 6,7) IN ('10', '11', '12') THEN '4Q' END AS QUARTER, COUNT(..
๐Ÿ“‘ 1. ๋ฌธ์ œ์„ค๋ช…โŒ 2. ์‹คํŒจํ•œ ์‹œ๋„SELECT U.User_id, U.Nickname, CONCAT(U.City,' ', U.Street_address1, ' ', U.Street_address2) AS ์ „์ฒด์ฃผ์†Œ, CONCAT(SUBSTR(TLNO, 1, 3), '-', SUBSTR(TLNO, 4, 4), '-', SUBSTR(TLNO, 8)) AS ์ „ํ™”๋ฒˆํ˜ธFROM Used_goods_board B JOIN Used_goods_user U ON B.Writer_id = U.User_idHAVING COUNT(BOARD_ID) >= 3ORDER BY U.User_id DESC; - CONCAT ํ•จ์ˆ˜๋Š” + ๊ฐ€ ์•„๋‹ˆ๋ผ , ๋ฅผ ์‚ฌ์šฉ..
๐Ÿ“‘ 1. ๋ฌธ์ œ์„ค๋ช…โŒ 2. ์‹คํŒจํ•œ ์‹œ๋„์ฝ”๋“œ๋Š” ์ž‘๋™ํ•˜์ง€๋งŒ ์ •๋‹ต ์ฒ˜๋ฆฌ X์ด์œ : CAR_ID ์ค‘๋ณต๋จSELECT A.Car_idFROM Car_rental_company_car A JOIN Car_rental_company_rental_history B ON A.Car_id = B.Car_idWHERE A.Car_type = '์„ธ๋‹จ' AND B.Start_date BETWEEN '2022-10-01' AND '2022-10-31'ORDER BY A.Car_id DESC;โญ 3. ์ •๋‹ต์ฝ”๋“œCAR_ID ์ค‘๋ณต์ด ์—†์–ด์•ผ ํ•˜๋ฉฐ -> DISTINCT๋Œ€์—ฌ ๊ธฐ๋ก์ด ์žˆ๋Š” -> ON A.CAR_ID = B.CAR_IDSELECT DISTINCT(A.Car_id)FROM ..
์ธ์ ˆ๋ฏธ์˜€๋˜๊ฒƒ
'SQL' ํƒœ๊ทธ์˜ ๊ธ€ ๋ชฉ๋ก
์ƒ๋‹จ์œผ๋กœ