Table Name : info
+---------------+-------------+------+--------+
| id | currentdate | hour | minute | 그 외의 수치 데이터 컬럼들
+---------------+-------------+------+--------+
| Gusto | 1 | 13 | 24 |
| Gusto | 1 | 13 | 25 |
| Gusto | 1 | 13 | 26 |
| Gusto | 1 | 13 | 27 |
| Gusto | 1 | 13 | 28 |
| Gusto | 1 | 13 | 29 |
| Gusto | 1 | 13 | 30 |
| Gusto | 1 | 13 | 31 |
| Gusto | 1 | 13 | 32 |
| Gusto | 1 | 13 | 33 |
+---------------+-------------+------+--------+
currentdate 범위 : 1 ~ 8
hour 범위 : 0 ~ 23
minute 범위 : 0 ~ 59
다음과 같은 테이블에서 다음 조건으로 검색을 해야합니다.
1. id 별로 데이터를 검색해야함
2. currentdate = 8 일 경우에만 currentdate 2, hour 0 부터 currentdate 3, hour 10 범위의 데이터를 검색해야함
3. 값은 해당 범위의 데이터의 평균 및 합계로 계산해야함
급하게 데이터가 필요해서
select id, currentdate, hour
from info
where currentdate in (2, 3)
group by id
다음의 쿼리로 currentdate 2,3 인 데이터를 뽑아서 엑셀로 수정해서 사용했습니다. 엑셀로 급하게 하기는 했지만 위 조건의 쿼리를 계속 짜보려고 하는데 쉽게 되질 않네요.
select id, currentdate, hour
from info A
where A.id in (select id from info B where B.currentdate > 7) and A.currentdate in (2, 3)
로 2번 조건은 앞부분은 해결 되는데... 2번 뒤 범위 조건이 문제네요.
rain748님, 창조의 날개님 답변 감사합니다.
where currentdate = 2 OR (currentdate = 3 and hour between 0 and 10)
와
and ( (currentdate = 2 and hour between 0 and 23) or (currentdate = 3 and hour between 0 and 10))
두 가지 다 했었는데 다른 점은 OR을 안 넣고 AND로 넣어서 계속 안 됐었던거네요...
이전에는 이렇게 쿼리를 하다가 안됐었습니다.
where currentdate = 2 AND (currentdate = 3 and hour between 0 and 10)
와
and ( (currentdate = 2 and hour between 0 and 23) AND (currentdate = 3 and hour between 0 and 10))
답변 감사합니다! OR의 소중함을 알고가네요 ㅠㅠ