안녕하세요 선배님들 에어컨 밑에서 시원하게 개발들 하고 계신가요 ㅎㅎ
오키에도 올렸는데 정리자 잘안되서 질문하러 왔습니다 ㅎㅎ
게시판 리스트에 뿌릴 select 쿼리를 짜야하는데 요구 조건이
특정 기간동안 각 일자별 00시~07시, 01시~08시 .... 16시~23시로 묶어서 평균을 내는데(일자별로 17개의 row가 나옵니다)
각 row별 컬럼1, 2, 3, 4... 중 가장 높은 값을 묶어서 해당 일자의 8시간 평균값 이라는 한개의 row를 만들어야 합니다
특정 기간의 범위는 제한이 없고 00시~23시까지 데이터에 공백은 없습니다.
어떻게 짜면 좋을까요.. 답이 아니라 힌트라도 주시면 감사하겠습니다 ㅎㅎ
좋은 하루 되세요~
--------------------------추가--------------------------------------------
날짜 시간 값1 값2 값3 값4 값5 값6
2018.08.07 00 0.006 0.9 0.019 85.0 43.0 0.021
2018.08.07 01 0.01 1.1 0.025 72.0 28.0 0.012
2018.08.07 02 0.01 0.6 0.01 52.0 38.0 0.023
2018.08.07 03 0.009 0.7 0.01 48.0 37.0 0.02
2018.08.07 04 0.008 1.0 0.007 43.0 42.0 0.024
2018.08.07 05 0.007 0.3 0.005 87.0 46.0 0.028
2018.08.07 06 0.006 0.6 0.007 36.0 28.0 0.027
2018.08.07 07 0.006 0.9 0.019 85.0 43.0 0.021
2018.08.07 08 0.01 1.0 0.025 72.0 28.0 0.012
2018.08.07 09 0.01 0.6 0.01 52.0 38.0 0.023
2018.08.07 10 0.009 0.7 0.01 48.0 37.0 0.02
2018.08.07 11 0.008 1.0 0.007 43.0 42.0 0.024
2018.08.07 12 0.007 0.3 0.005 87.0 46.0 0.028
2018.08.07 13 0.006 0.6 0.007 36.0 28.0 0.027
2018.08.07 14 0.006 0.9 0.019 85.0 43.0 0.021
2018.08.07 15 0.01 1.0 0.025 72.0 28.0 0.012
2018.08.07 16 0.01 0.6 0.01 52.0 38.0 0.023
2018.08.07 17 0.009 0.7 0.01 48.0 37.0 0.02
2018.08.07 18 0.008 1.0 0.007 43.0 42.0 0.024
2018.08.07 19 0.007 0.3 0.005 87.0 46.0 0.028
2018.08.07 20 0.006 0.6 0.007 36.0 28.0 0.027
2018.08.07 21 0.008 1.0 0.007 43.0 42.0 0.024
2018.08.07 22 0.007 0.3 0.005 87.0 46.0 0.028
2018.08.07 23 0.006 0.6 0.007 36.0 28.0 0.027
......
위와 같은 테이블이 있고
시간범위별로 00~07시 평균(값1), 평균(값2)... 01~08시 평균(값1), 평균(값2).... 16~23시 평균(값1), 평균(값2)... 까지 구한뒤
일자별로 가장 큰값으로 한개의 row를 만들어야 합니다 ㅠㅠ;
설명이 제대로 됐는지 모르겠지만 요구된 내용 그대로 수정했습니다 ㅎㅎ;
WITH t(ymd, hh, v1, v2, v3, v4, v5, v6) AS ( SELECT '2018.08.07', '00', 0.006, 0.9, 0.019, 85.0, 43.0, 0.021 FROM dual UNION ALL SELECT '2018.08.07', '01', 0.010, 1.1, 0.025, 72.0, 28.0, 0.012 FROM dual UNION ALL SELECT '2018.08.07', '02', 0.010, 0.6, 0.010, 52.0, 38.0, 0.023 FROM dual UNION ALL SELECT '2018.08.07', '03', 0.009, 0.7, 0.010, 48.0, 37.0, 0.020 FROM dual UNION ALL SELECT '2018.08.07', '04', 0.008, 1.0, 0.007, 43.0, 42.0, 0.024 FROM dual UNION ALL SELECT '2018.08.07', '05', 0.007, 0.3, 0.005, 87.0, 46.0, 0.028 FROM dual UNION ALL SELECT '2018.08.07', '06', 0.006, 0.6, 0.007, 36.0, 28.0, 0.027 FROM dual UNION ALL SELECT '2018.08.07', '07', 0.006, 0.9, 0.019, 85.0, 43.0, 0.021 FROM dual UNION ALL SELECT '2018.08.07', '08', 0.010, 1.0, 0.025, 72.0, 28.0, 0.012 FROM dual UNION ALL SELECT '2018.08.07', '09', 0.010, 0.6, 0.010, 52.0, 38.0, 0.023 FROM dual UNION ALL SELECT '2018.08.07', '10', 0.009, 0.7, 0.010, 48.0, 37.0, 0.020 FROM dual UNION ALL SELECT '2018.08.07', '11', 0.008, 1.0, 0.007, 43.0, 42.0, 0.024 FROM dual UNION ALL SELECT '2018.08.07', '12', 0.007, 0.3, 0.005, 87.0, 46.0, 0.028 FROM dual UNION ALL SELECT '2018.08.07', '13', 0.006, 0.6, 0.007, 36.0, 28.0, 0.027 FROM dual UNION ALL SELECT '2018.08.07', '14', 0.006, 0.9, 0.019, 85.0, 43.0, 0.021 FROM dual UNION ALL SELECT '2018.08.07', '15', 0.010, 1.0, 0.025, 72.0, 28.0, 0.012 FROM dual UNION ALL SELECT '2018.08.07', '16', 0.010, 0.6, 0.010, 52.0, 38.0, 0.023 FROM dual UNION ALL SELECT '2018.08.07', '17', 0.009, 0.7, 0.010, 48.0, 37.0, 0.020 FROM dual UNION ALL SELECT '2018.08.07', '18', 0.008, 1.0, 0.007, 43.0, 42.0, 0.024 FROM dual UNION ALL SELECT '2018.08.07', '19', 0.007, 0.3, 0.005, 87.0, 46.0, 0.028 FROM dual UNION ALL SELECT '2018.08.07', '20', 0.006, 0.6, 0.007, 36.0, 28.0, 0.027 FROM dual UNION ALL SELECT '2018.08.07', '21', 0.008, 1.0, 0.007, 43.0, 42.0, 0.024 FROM dual UNION ALL SELECT '2018.08.07', '22', 0.007, 0.3, 0.005, 87.0, 46.0, 0.028 FROM dual UNION ALL SELECT '2018.08.07', '23', 0.006, 0.6, 0.007, 36.0, 28.0, 0.027 FROM dual ) SELECT ymd , ROUND(MAX(v1), 6) v1 , ROUND(MAX(v2), 6) v2 , ROUND(MAX(v3), 6) v3 , ROUND(MAX(v4), 6) v4 , ROUND(MAX(v5), 6) v5 , ROUND(MAX(v6), 6) v6 FROM (SELECT ymd, hh , AVG(v1) OVER(ORDER BY ymd, hh ROWS 7 PRECEDING) v1 , AVG(v2) OVER(ORDER BY ymd, hh ROWS 7 PRECEDING) v2 , AVG(v3) OVER(ORDER BY ymd, hh ROWS 7 PRECEDING) v3 , AVG(v4) OVER(ORDER BY ymd, hh ROWS 7 PRECEDING) v4 , AVG(v5) OVER(ORDER BY ymd, hh ROWS 7 PRECEDING) v5 , AVG(v6) OVER(ORDER BY ymd, hh ROWS 7 PRECEDING) v6 FROM t ) WHERE hh >= '07' GROUP BY ymd ORDER BY ymd ;