본문 바로가기

SQL/코딩테스트16

[MySQL] 그룹변수가 2개로 피벗테이블 구성 문제 : 아래와 같은 데이터가 있을 때 ID와, Month를 그룹으로하여 revenue의 합계를 구하는 피벗테이블을 구성해라. 이때 Month값이 열로 와야하며, 변수명은 다음과 같아야 한다.변수명 : Jan_Revenue | Feb_Revenue | Mar_Revenue | ... | Dec_Revenue/* INPUT TABLE| id | revenue | month || -- | ------- | ----- || 1 | 8000 | Jan || 2 | 9000 | Jan || 3 | 10000 | Feb || 1 | 7000 | Feb || 1 | 6000 | Mar |*//* OUTPUT TABLE */+------+-------------+--.. 2024. 11. 5.
[MySQL] SQL로 피벗테이블 만드는 순서(데이터 피봇팅) 그룹변수 하나와 양적변수 여러개로 피벗을 만들고 싶을 때가 있다. 데이터 중  아래 두 변수를 활용해 피벗을 만들어 보겠다. 문제 : 그룹별로 가격의 평균을 보고 싶다.코드 한줄로 끝내도 되나 좀더 자세히 풀어보겠다. 풀이 예시는 그룹 1에 대한 평균을 계산하는것이다. 풀이 절차1. 핵심은 CategoryID가 1일때 Category1_Price 가격을 출력하고 나머지 그룹엔 NULL로 작성하는 것이다. 이렇게 하면 차후 통계량 요약시 해당 행은 계산식에 포함되지 않는다.(다른 그룹이 NULL임을 보이기 위해 *를 추가하여 전체 데이터를 보였다. 실제 피벗에서는 *를 빼야함에 유의.)SELECT *, CASE WHEN categoryid = 1 THEN price ELSE NULL END AS categ.. 2024. 11. 5.
[MySQL] CASE 문에서 WHEN 순서의 중요성 문제 내용 : Write a query identifying the type of each record in the TRIANGLES table using its three side lengths. Output one of the following statements for each record in the table: Equilateral: It's a triangle with  sides of equal length. Isosceles: It's a triangle with  sides of equal length. Scalene: It's a triangle with  sides of differing lengths. Not A Triangle: The given values of A, B, and.. 2024. 11. 4.
[MySQL] 그룹화된 변수에 조건을 걸어 원하는 값 출력하기 문제 내용 : We define an employee's total earnings to be their monthly Salary X Months worked, and the maximum total earnings to be the maximum total earnings for any employee in the Employee table. Write a query to find the maximum total earnings for all employees as well as the total number of employees who have maximum total earnings. Then print these values as 2 space-separated integers. 해석 : 직원.. 2024. 11. 4.