아래 글을 올렸는데요... 다시 질문을 올립니다.
제가 전체적으로 이해를 못하고... 이것저것을 참고해서 하려다 보니 결과값이 안나오네요...
계약 기간
WITH SOTable AS
(
SELECT 'A' AS '제목', CONVERT(DATETIME,'2011-03-29') AS '시작시간', CONVERT(DATETIME,'2012-03-29') AS '종료시간'
)
SELECT
DAT.*, AAA.*
FROM
(
SELECT
*
, DATEDIFF(MONTH,시작시간,종료시간) + 1 AS TotalCount
FROM SOTable
) DAT
Inner join
(
SELECT TOP 300
ROW_NUMBER() OVER(ORDER BY number) AS NO
, convert(varchar(10),DATEADD(Month, ROW_NUMBER() OVER(ORDER BY number) - 1, CONVERT(DATETIME,'2010-03-01')) , 120) AS StartDate
, convert(varchar(10),DATEADD(Month, ROW_NUMBER() OVER(ORDER BY number) - 1, CONVERT(DATETIME,'2010-03-31')), 120) AS EndDate
FROM master.dbo.spt_values
) AAA
ON convert(varchar(7), DAT.시작시간, 120) >= convert(varchar(7),AAA.EndDate , 120) and convert(varchar(7), DAT.종료시간, 120) >= convert(varchar(7),AAA.EndDate , 120)
order by 제목
이렇게 쿼리를 하면 결과 값이
A 2011-03-29 00:00:00.000 2012-03-29 00:00:00.000 13 1 2010-03-01 2010-03-31
A 2011-03-29 00:00:00.000 2012-03-29 00:00:00.000 13 2 2010-04-01 2010-04-30
A 2011-03-29 00:00:00.000 2012-03-29 00:00:00.000 13 3 2010-05-01 2010-05-31
A 2011-03-29 00:00:00.000 2012-03-29 00:00:00.000 13 4 2010-06-01 2010-06-30
A 2011-03-29 00:00:00.000 2012-03-29 00:00:00.000 13 5 2010-07-01 2010-07-31
A 2011-03-29 00:00:00.000 2012-03-29 00:00:00.000 13 6 2010-08-01 2010-08-31
A 2011-03-29 00:00:00.000 2012-03-29 00:00:00.000 13 7 2010-09-01 2010-09-30
A 2011-03-29 00:00:00.000 2012-03-29 00:00:00.000 13 8 2010-10-01 2010-10-31
A 2011-03-29 00:00:00.000 2012-03-29 00:00:00.000 13 9 2010-11-01 2010-11-30
A 2011-03-29 00:00:00.000 2012-03-29 00:00:00.000 13 10 2010-12-01 2010-12-31
A 2011-03-29 00:00:00.000 2012-03-29 00:00:00.000 13 11 2011-01-01 2011-01-31
A 2011-03-29 00:00:00.000 2012-03-29 00:00:00.000 13 12 2011-02-01 2011-02-28
A 2011-03-29 00:00:00.000 2012-03-29 00:00:00.000 13 13 2011-03-01 2011-03-31
이렇게 나오는데요.
결과값 갯수는 맞는데..
전 이 결과값이
A | 2011-03-29 | 2011-03-31 |
A | 2011-04-01 | 2011-04-30 |
A | 2011-05-01 | 2011-05-31 |
A | 2011-06-01 | 2011-06-30 |
A | 2011-07-01 | 2011-07-31 |
A | 2011-08-01 | 2011-08-31 |
A | 2011-09-01 | 2011-09-30 |
A | 2011-10-01 | 2011-10-31 |
A | 2011-05-01 | 2011-05-31 |
A | 2011-12-01 | 2011-12-31 |
A | 2012-01-01 | 2012-01-31 |
A | 2012-02-01 | 2011-02-28 |
A | 2012-03-01 | 2012-03-31 |
이렇게 나오게 하고 싶은데.. 어떻게 해야 할까요?
Comment 3
-
초보자임당
2014.04.22 13:38
WITH SOTable AS(SELECT 'A' AS '제목', CONVERT(DATETIME,'2011-03-29') AS '시작시간', CONVERT(DATETIME,'2012-03-29') AS '종료시간')SELECTDAT.[제목], StartDate, EndDateFROM(SELECT*, DATEDIFF(MONTH,시작시간,종료시간) + 1 AS TotalCountFROM SOTable) DATInner join(SELECT TOP 300ROW_NUMBER() OVER(ORDER BY number) AS NO, convert(varchar(10),DATEADD(Month, ROW_NUMBER() OVER(ORDER BY number) - 1, CONVERT(DATETIME,'2010-03-01')) , 120) AS StartDate, convert(varchar(10),DATEADD(Month, ROW_NUMBER() OVER(ORDER BY number) - 1, CONVERT(DATETIME,'2010-03-31')), 120) AS EndDateFROM master.dbo.spt_values) AAAON convert(varchar(7), DAT.시작시간, 120) >= convert(varchar(7),AAA.EndDate , 120) and convert(varchar(7), DAT.종료시간, 120) >= convert(varchar(7),AAA.EndDate , 120)order by 제목 -
taz2315
2014.04.22 13:51
답변을 달아주신 쿼리를 실행시켜 보면 결과값이 2010-03-01 부터 시작이 되게 나옵니다.
결과값이 '2011-03-29 부터 나와야 합니다.
-
건우아빠
2014.04.22 20:16