본문 바로가기
Language/SQL

[SQL 연습] 프로그래머스 테스트 KIT (GROUP BY)

by soccerman 2020. 10. 28.
반응형

GROUP BY 관련 정리 잘 되어있는 블로그 발견

extbrain.tistory.com/56

 

[MySQL] 그룹화하여 데이터 조회 (GROUP BY)

▶MySQL 그룹화하여 데이터 조회 (GROUP BY) ▶설명 하나, 예를 들어보겠습니다. MySQL에서 유형별로 갯수를 가져오고 싶은데, 단순히 COUNT 함수로 데이터를 조회하면 전체 갯수만을 가져옵니다. 이렇

extbrain.tistory.com

 

WHERE은 그룹화 전 조건 

HAVING은 그룹화 후 조건 

 

LEFT JOIN : SELECT된 왼쪽 테이블을 기준으로 합침

 

쿼리문 설명

cte 테이블과ANIMAL_OUTS 테이블을 합쳐서 cte.hour과 count(ani.ANIMAL_ID) 열을 SELECT함

LEFT JOIN 이기 때문에 조건 ON에 해당하는 값이 0 혹은 NULL이더라도 cte.hour 값은 왼쪽 열에 나타나고 오른쪽 HOUR(ani.DATETIME) 값은 우측 열에 나타남.

 

LEFT JOINJOIN으로 하는 경우 각각 실행결과 차이

LEFT JOIN 사용시 결과
JOIN 사용시 결과

참고자료

1. mysql date and time function doc

dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html

 

MySQL :: MySQL 8.0 Reference Manual :: 12.7 Date and Time Functions

12.7 Date and Time Functions This section describes the functions that can be used to manipulate temporal values. See Section 11.2, “Date and Time Data Types”, for a description of the range of values each date and time type has and the valid formats

dev.mysql.com

2. WITH RECURSIVE 구문

dev.mysql.com/doc/refman/8.0/en/with.html#common-table-expressions-recursive

 

MySQL :: MySQL 8.0 Reference Manual :: 13.2.15 WITH (Common Table Expressions)

13.2.15 WITH (Common Table Expressions) A common table expression (CTE) is a named temporary result set that exists within the scope of a single statement and that can be referred to later within that statement, possibly multiple times. The following disc

dev.mysql.com

반응형

댓글