현재 mysql 로 Select 를 줘서,
SELECT * FROM TEST WHERE date between '2021-04-05' and '2021-04-10'
이런식으로 날짜 구간을 지정해줬을 때, 다음과 같은 피벗 형태로 나오게끔 구현하고 싶습니다.
id | 2021-04-05 | 2021-04-06 | 2021-04-07 | 2021-04-08 | 2021-04-09 | 2021-04-10 |
test1 | ||||||
test2 |
아래 코드는 제가 현재까지 구현한 코드이며, 아래코드의 문제는 일자가 많을 경우 일일이
경우의 수를 잡기가 힘들다는 것입니다.
SELECT '' AS USERCD , MAX(IF(B.ROWNUM = '1', b.datatime,'-')) AS sensor_date1 , MAX(IF(B.ROWNUM = '2', b.datatime, '-')) AS sensor_date2 , MAX(IF(B.ROWNUM = '3', b.datatime, '-')) AS sensor_date3 , MAX(IF(B.ROWNUM = '4', b.datatime, '-')) AS sensor_date4 , MAX(IF(B.ROWNUM = '5', b.datatime, '-')) AS sensor_date5 , MAX(IF(B.ROWNUM = '6', b.datatime, '-')) AS sensor_date6 , MAX(IF(B.ROWNUM = '7', b.datatime, '-')) AS sensor_date7 , MAX(IF(B.ROWNUM = '8', b.datatime, '-')) AS sensor_date8 , MAX(IF(B.ROWNUM = '9', b.datatime, '-')) AS sensor_date9 , MAX(IF(B.ROWNUM = '10', b.datatime, '-')) AS sensor_date10 , MAX(IF(B.ROWNUM = '11', b.datatime, '-')) AS sensor_date11 , MAX(IF(B.ROWNUM = '12', b.datatime, '-')) AS sensor_date12 , MAX(IF(B.ROWNUM = '13', b.datatime, '-')) AS sensor_date13 , MAX(IF(B.ROWNUM = '14', b.datatime, '-')) AS sensor_date14 , MAX(IF(B.ROWNUM = '15', b.datatime, '-')) AS sensor_date15 , MAX(IF(B.ROWNUM = '16', b.datatime, '-')) AS sensor_date16 , MAX(IF(B.ROWNUM = '17', b.datatime, '-')) AS sensor_date17 , MAX(IF(B.ROWNUM = '18', b.datatime, '-')) AS sensor_date18 , MAX(IF(B.ROWNUM = '19', b.datatime, '-')) AS sensor_date19 , MAX(IF(B.ROWNUM = '20', b.datatime, '-')) AS sensor_date20 , MAX(IF(B.ROWNUM = '21', b.datatime, '-')) AS sensor_date21 , MAX(IF(B.ROWNUM = '22', b.datatime, '-')) AS sensor_date22 , MAX(IF(B.ROWNUM = '23', b.datatime, '-')) AS sensor_date23 , MAX(IF(B.ROWNUM = '24', b.datatime, '-')) AS sensor_date24 , MAX(IF(B.ROWNUM = '25', b.datatime, '-')) AS sensor_date25 , MAX(IF(B.ROWNUM = '26', b.datatime, '-')) AS sensor_date26 , MAX(IF(B.ROWNUM = '27', b.datatime, '-')) AS sensor_date27 , MAX(IF(B.ROWNUM = '28', b.datatime, '-')) AS sensor_date28 , MAX(IF(B.ROWNUM = '29', b.datatime, '-')) AS sensor_date29 , MAX(IF(B.ROWNUM = '30', b.datatime, '-')) AS sensor_date30 , MAX(IF(B.ROWNUM = '31', b.datatime, '-')) AS sensor_date31 FROM ( SELECT datatime,SUBSTRING(a.datatime,9,2) AS ROWNUM FROM stink_main a WHERE a.datatime BETWEEN '2021-04-05' AND '2021-07-30' ) B
날짜 구간을 반복을 하면서 날짜별 컬럼을 만들어 집합을 만들어주고 싶은데, 다른 방법이 없을까요?
관련하여 아무리 구글링해도 찾기가 힘들고 MySQL 자체에 피벗기능이 없다고 나오니
이 기능이 되는건가 의문도 들고.. 도움부탁드립니다! 선배님들 !