다중조건에 대한 범위 검색이 가능할까요? 0 13 5,308

by Gusto mysql 다중 조건 범위 [2015.04.07 17:22:27]


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번 뒤 범위 조건이 문제네요.

by rain748 [2015.04.07 17:39:03]

and hour between o and 10


by Gusto [2015.04.07 17:44:22]

그 조건일 경우에 currentdate 가 2, 3 둘 다 hour 가 0 ~ 10 으로 한정됩니다.

currentdate = 2 hour = 0 ~ 23 과 currentdate = 3 hour 0 ~ 10 이 검색되어야 합니다.

쿼리로 보자면

where currentdate = 2 일때와 where currentdate = 3 and hour between 0 and 10

의 쿼리가 합쳐져야 합니다.


by 창조의날개 [2015.04.07 17:50:26]

where currentdate = 2 OR (currentdate = 3 and hour between 0 and 10)


by 창조의날개 [2015.04.07 17:49:00]


where A.id in (select id from info B where B.currentdate > 7) and A.currentdate in (2, 3)
조건을 만족하는 데이터가 있나요?
info 테이블에 currentdate 값이 7보다 크면서 2이거나 3인 경우가 있을지??

 


by Gusto [2015.04.07 17:52:26]

where A.id in (select id from info B where B.currentdate > 7) and A.currentdate in (2, 3)

A.id in (select id from info B where B.currentdate > 7) 에서 currentdate 가 8 이상인 id 들을 뽑아오고, 그 아이디들 중에서 currentdate 가 2, 3인 걸로 추리는 쿼리입니다.


by rain748 [2015.04.07 17:50:51]

and ( (currentdate = 2 and hour between 0 and 23) or (currentdate = 3 and hour between 0 and 10))

 


by Gusto [2015.04.07 17:56:06]

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의 소중함을 알고가네요 ㅠㅠ


by 마농 [2015.04.07 17:59:04]

말씀하신 8 은 데이터 값이 아닌 검색 조건값인 듯 하네요.
실제 데이터에 8 이 들어 있지는 않은 듯 합니다.
검색 조건으로 1~7 까지가 들어오면 해당 값을 검색하고
8 이 들어오면 특별한 조건으로 검색하는 듯 하네요.
맞나요?


by Gusto [2015.04.07 18:37:44]
모바일이라 마농님 댓글에 답글이 안되어서 이렇게 남깁니다.
위 쿼리에서는 조건값으로 사용된게 맞습니다. 실제 데이터는 다음과 같이 데이터가 입력되어 있습니다.
테이블에 id마다 currentdate 컬럼의 값이 다릅니다. 예를 들면 id가 A 인 사람은 currentdate 가 1~8까지, B 인 사람은 currentdate 가 1~4까지 입력되어 있습니다.
그래서 8까지 모두 입력된 id만을 찾기 위해 A.id in (select B.id from info B where currentdate > 7) 로 검색 조건을 주었습니다.

맞게 답변한건지 헤깔리네요.

확인하시면 코맨트 부탁드립니다.

by 마농 [2015.04.07 18:54:02]

더 헷갈리네요?

8을 가진 id 에 한해서만 2,3을 조회하라는 건지? <--- 고정 조건

이럴땐 이렇게 뽑고 저럴땐 저렇게 뽑는 가변 조건이 적용되야 하는건지? <-- 가변조건


by Gusto [2015.04.07 18:59:37]
8을 가진 id에 한해서만 currentdate 2, 3을 조회하는 고정조건입니다.

by 마농 [2015.04.07 19:04:57]
SELECT *
  FROM info
 WHERE id IN (SELECT id FROM info WHERE currentdate = 8)
   AND currentdate IN (2, 3)
   AND hour <= DECODE(currentdate, 2, 23, 3, 10)
;

 


by Gusto [2015.04.07 20:52:40]

마농님 답변 감사합니다.

댓글등록
SQL문을 포맷에 맞게(깔끔하게) 등록하려면 code() 버튼을 클릭하여 작성 하시면 됩니다.
로그인 사용자만 댓글을 작성 할 수 있습니다. 로그인, 회원가입