본문 바로가기

DB/MariaDB

MariaDB - 달력(Calendar) 출력하기 (날짜 주 단위로 출력)

반응형

2024년 5월( 2024-05-01 ~ 2024-05-31 ) 날짜 출력

WITH RECURSIVE DATE_TEMP AS (
	SELECT DATE_FORMAT('2024-05-01', '%Y%m%d') AS DT
		 , WEEKDAY('2024-05-01') AS WEEKDAY
		 , WEEK('2024-05-01') AS WEEK
	 UNION ALL
	SELECT DATE_FORMAT(DATE_ADD(DATE_TEMP.DT, INTERVAL 1 DAY), '%Y%m%d') AS DT
		 , WEEKDAY(DATE_ADD(DATE_TEMP.DT, INTERVAL 1 DAY)) AS WEEKDAY
		 , WEEK(DATE_ADD(DATE_TEMP.DT, INTERVAL 1 DAY)) AS WEEK
	  FROM DATE_TEMP
     WHERE DATE_ADD(DATE_TEMP.DT, INTERVAL 1 DAY) <= DATE('2024-05-31')
)
SELECT DATE_FORMAT(MIN(CASE WHEN WEEKDAY = 6 THEN TB.DT END), '%d') AS SUN /* %d:01, %e:1 */
	 , DATE_FORMAT(MIN(CASE WHEN WEEKDAY = 0 THEN TB.DT END), '%d') AS MON
	 , DATE_FORMAT(MIN(CASE WHEN WEEKDAY = 1 THEN TB.DT END), '%d') AS TUE
	 , DATE_FORMAT(MIN(CASE WHEN WEEKDAY = 2 THEN TB.DT END), '%d') AS WEN
	 , DATE_FORMAT(MIN(CASE WHEN WEEKDAY = 3 THEN TB.DT END), '%d') AS THU
	 , DATE_FORMAT(MIN(CASE WHEN WEEKDAY = 4 THEN TB.DT END), '%d') AS FRI
	 , DATE_FORMAT(MIN(CASE WHEN WEEKDAY = 5 THEN TB.DT END), '%d') AS STA
  FROM DATE_TEMP TB
 GROUP BY TB.WEEK
 ORDER BY TB.WEEK
;

 

반응형

'DB > MariaDB' 카테고리의 다른 글

[MariaDB] 컬럼명 카멜 케이스로 출력하기  (0) 2024.12.05