dev_beomgeun
꾸준하게 차근차근
dev_beomgeun
전체 방문자
오늘
어제
  • 분류 전체보기 (170)
    • 전공 (0)
      • 운영체제 (0)
      • 알고리즘 (0)
      • 자료구조 (0)
      • 데이터베이스 (0)
      • 네트워크 (0)
    • 개발 공부 (32)
      • 웹 (6)
      • 리눅스 (4)
      • 머신러닝 (1)
      • 스프링 (17)
      • Git (2)
      • AWS (2)
    • 개발 도서, 강의 (3)
      • 스프링 입문을 위한 자바 객체지향의 원리와 이해 (0)
      • 모든 개발자를 위한 HTTP 웹 기본 지식(김영한.. (2)
      • 스프링 부트와 AWS로 혼자 구현하는 웹서비스 (1)
    • 문제 풀이 (118)
      • 백준 알고리즘 (72)
      • 프로그래머스 알고리즘, SQL (38)
      • Hackerrank SQL (8)
    • 프로젝트 기록 (4)
      • 캡스톤 종합설계 (4)
      • 반려하루 프로젝트 (0)
    • 활동 기록 (12)
      • 네이버 부스트캠프 (7)
      • 취준 & 코테 (4)
      • 소프트웨어 마에스트로 13기 (1)
    • 이것저것 (1)

블로그 메뉴

  • 홈
  • 깃허브
  • 링크드인
  • 방명록

공지사항

인기 글

태그

  • c++
  • 네이버 부스트캠프
  • 서블릿
  • 프로그래머스 SQL
  • 스프링
  • 부스트캠프
  • Baekjoon
  • Hackerrank
  • 프로그래머스
  • 그래프탐색
  • 백준 DP
  • 기록
  • HackerRank mysql
  • 회고
  • 백준
  • 반성
  • dp
  • AI Tech
  • 일기
  • BFS

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
dev_beomgeun

꾸준하게 차근차근

문제 풀이/Hackerrank SQL

[Hackerrank SQL] Ollivander's Inventory

2021. 2. 24. 18:36
728x90

www.hackerrank.com/challenges/harry-potter-and-wands/problem

 

Ollivander's Inventory | HackerRank

Help pick out Ron's new wand.

www.hackerrank.com

 

Harry Potter and his friends are at Ollivander's with Ron, finally replacing Charlie's old broken wand.

Hermione decides the best way to choose is by determining the minimum number of gold galleons needed to buy each non-evil wand of high power and age. Write a query to print the id, age, coins_needed, and power of the wands that Ron's interested in, sorted in order of descending power. If more than one wand has same power, sort the result in order of descending age.

 

슬슬 어려워진다.. 단순히 join 해서 풀면 되는 줄 알았는데, 알고 보니까 각 code(=age)와 power가 같을 경우 제일 싼 지팡이만 선택해야 한다.

 

결국 join 해서 age와 power로 group by 해서 어떻게 해보려고 했는데 실패했다.

 

방법은

1. 내가 필요한 정보를 담은 커스텀 테이블을 하나 만들어서

2. 그 테이블과 join 해서

3. 각 케이스(code-power가 같은 경우)에 최솟값을 갖고 있는 데이터를 만들고

4. property 테이블과 join 해서 age를 얻은 후 정렬, 출력한다.

 

SELECT w.ID, P.AGE, m.coins_needed, w.power
from (SELECT code, min(coins_needed) as coins_needed, power from wands group by code, power) as m
inner join wands as w
on w.code = m.code and w.power = m.power and w.coins_needed = m.coins_needed
inner join wands_property as p
on p.code = w.code
where p.is_evil = 0
order by w.power desc, p.age desc

각 케이스별로 최소 금액을 출력하려면 group by로 code, power를 해줘야 하는데 ID가 껴있을 경우 group by 가 되지 않으므로 ID를 뺀 상태로 group by, 최소 금액을 가진 테이블을 만든다. (첫 from 안의 테이블 정의)

이후 ID값을 다시 더해주기 위해 기본 wands 테이블과 join 해주고 (첫 번째 join)

age값을 더해주기 위해서 property 테이블과 join 해준다. (두 번째 join)

이후 조건과 정렬을 해주면 된다.

728x90
저작자표시 비영리 변경금지

'문제 풀이 > Hackerrank SQL' 카테고리의 다른 글

[Hackerrank SQL] The Report  (0) 2021.02.24
[Hackerrank SQL] Average Population of Each Continent  (0) 2021.02.24
[Hackerrank SQL] Type of Triangle  (0) 2021.02.24
[Hackerrank SQL] Weather Observation Station 13  (0) 2021.02.23
[Hackerrank SQL] Weather Observation Station 8  (0) 2021.02.23
    '문제 풀이/Hackerrank SQL' 카테고리의 다른 글
    • [Hackerrank SQL] The Report
    • [Hackerrank SQL] Average Population of Each Continent
    • [Hackerrank SQL] Type of Triangle
    • [Hackerrank SQL] Weather Observation Station 13
    dev_beomgeun
    dev_beomgeun
    백엔드 개발을 하며 얻은 지식과 경험을 공유합니다. 현재 카카오페이에서 백엔드 엔지니어로 일하고 있습니다.

    티스토리툴바