database.sarang.net
UserID
Passwd
Database
DBMS
ㆍMySQL
PostgreSQL
Firebird
Oracle
Informix
Sybase
MS-SQL
DB2
Cache
CUBRID
LDAP
ALTIBASE
Tibero
DB 문서들
스터디
Community
공지사항
자유게시판
구인|구직
DSN 갤러리
도움주신분들
Admin
운영게시판
최근게시물
MySQL Q&A 30559 게시물 읽기
No. 30559
두 기간 중복 날짜 비교
작성자
이한나(seay)
작성일
2013-02-27 15:19
조회수
11,300

나름 해결해보려 고민했지만 쉽사리 잘 모르겠네요 ;;;

 

================================

예약번호 |  시작일       | 종료일

================================

1            | 2012-02-01 | 2012-06-30

2            | 2012-08-01 | 2012-12-31

3            | 2013-01-01 | 2013-12-31

 

위와 같은 테이블이 있을 경우 입력 조건으로

시작일 : 2012-06-29 

종료일 : 2012-08-03

로 검색할 경우 원하는 결과는 아래와 같습니다.

 

================================

예약번호 |  중복일 

================================

1            | 2

2            | 3

3            | 0

 

위와 같은 결과를 얻고 싶은데 어렵네요 ;;;;

답변 주시면 감사하겠습니다.

이 글에 대한 댓글이 총 4건 있습니다.

 중복일 이 어떤 기준으로 해서 2, 3 이 나오는건데요?

team b(teamb)님이 2013-02-27 15:54에 작성한 댓글입니다.

검색 날짜 범위 중 포함되는 요일이 몇일이냐 입니다.

1번 같은 경우 6월 29일 , 6월 30일 2일이 범위에 포함되므로 2를 

2번은 위와 같은 경우 처럼 3일이 포함되므로 3 이라고 나타내고 싶습니다. ^^

이한나(seay)님이 2013-02-27 16:18에 작성한 댓글입니다.
이 댓글은 2013-02-27 16:20에 마지막으로 수정되었습니다.

 대충 구현해봤습니다. 시작일, 종료일이 같은 년도-월 이 아니라면 대충 결과는 나옵니다만...

 

 

set @a_sdate='2012-06-29';

set @a_edate='2012-08-03';

select 

  예약번호

  ,case when 일자 > 조회일자 then datediff(일자,조회일자)+1

        when 일자 < 조회일자 then datediff(조회일자,일자)+1

        else 0 

   end as 중복일자

  from

  (

      select

      예약번호 

      

      ,case when date_format(시작일,'%Y-%m') = date_format(@a_sdate,'%Y-%m') then 시작일

            when date_format(종료일,'%Y-%m') = date_format(@a_sdate,'%Y-%m') then 종료일

            when date_format(시작일,'%Y-%m') = date_format(@a_edate,'%Y-%m') then 시작일

            when date_format(종료일,'%Y-%m') = date_format(@a_edate,'%Y-%m') then 종료일        

            else current_date 

       end as 일자

            

      ,case when date_format(시작일,'%Y-%m') = date_format(@a_sdate,'%Y-%m') then @a_sdate

            when date_format(종료일,'%Y-%m') = date_format(@a_sdate,'%Y-%m') then @a_sdate

            when date_format(시작일,'%Y-%m') = date_format(@a_edate,'%Y-%m') then @a_edate

            when date_format(종료일,'%Y-%m') = date_format(@a_edate,'%Y-%m') then @a_edate

            else current_date 

       end as 조회일자             

      from tbl_test

 

sqlfiddle.com/#!2/3365c/6

 

team b(teamb)님이 2013-02-28 11:12에 작성한 댓글입니다.

살짝 간단하게 고쳐 봤습니다.

항상 시작일 < 종료일(테이블, 검색조건 모두) 일 경우

set @a_sdate='2012-06-29';
set @a_edate='2012-08-03';

select case when e < @a_sdate then 0
       when s > @a_edate then 0
       else datediff(if(e<@a_edate,e, @a_edate), if(s>@a_sdate,s,@a_sdate) )+1
       end AS diff
from (      
  select s, e
  from ( select '2012-02-01' s, '2012-06-30' e from dual
         union all
         select '2012-08-01' s, '2012-12-31' e from dual
         union all
         select '2013-01-01' s, '2013-12-31' e from dual
        ) t
) tmp    ;  

이 쿼리로 확인 가능합니다.

 

select case when e < @a_sdate then 0
when s > @a_edate then 0
else datediff(if(e<@a_edate,e, @a_edate), if(s>@a_sdate,s,@a_sdate) )+1
end AS diff
from 테이블

위 쿼리를 해당 테이블, 검색조건에 맞게 고치시면 됩니다.

박인호(paerae)님이 2013-03-02 13:38에 작성한 댓글입니다.
[Top]
No.
제목
작성자
작성일
조회
30562where 절에서...
조외석
2013-03-06
8435
30561이중 Cursor문(Nested Cursor)문에서 Fetch 하는 방법??
김주성
2013-03-02
9747
30560mysqldump 문의 입니다. [2]
강삼수
2013-02-28
9696
30559두 기간 중복 날짜 비교 [4]
이한나
2013-02-27
11300
30558동접자 2만을 예상하고 있는데 DB 서버를 어떻게 구성해야 할지.. [7]
비됴
2013-02-26
11057
30556innodb와myisam차이점 [1]
김성환
2013-02-20
10485
30555안녕하십니까 프로그램때문에 뭐좀 여쭤보려고 합니다 [2]
꿈!
2013-02-15
8067
Valid XHTML 1.0!
All about the DATABASE... Copyleft 1999-2024 DSN, All rights reserved.
작업시간: 0.028초, 이곳 서비스는
	PostgreSQL v16.2로 자료를 관리합니다