TSQL

2015-01-13 4 views
0

에서 두 날짜 사이의 개월을 받기 나는 다음과 같은 반환하는 함수가 있습니다TSQL

Title  Start   End 
Task A 2015-01-02  2015-03-31 
Task B 2015-02-12  2015-04-01 
Task C 2014-11-01  2015-02-05 
.... 

내가 매달 열을 반환하고 싶은 일을하는 경우 그 시작과 달리 최종 기간 0

Title  Start   End   Jan Feb Mar Apr May Jun .... 
Task A 2015-01-02  2015-03-31 1  1 1 0 0 0 
Task B 2015-02-12  2015-04-01 0  1 1 1 0 0 
Task C 2014-11-01  2015-02-05 1  1 0 0 0 0 
.... 

누구나 어떻게 할 수 있습니까?

+2

당신이 요구하는 것은 PIVOT 테이블입니다. 조금 쳐다 보면 대답을 찾을 수 있습니다. 또는 적어도 문제를 해결하기 시작한 다음 시도한 쿼리를 게시 할 수 없으면 해결할 수 있도록 도와 드리겠습니다. –

+0

시작일과 종료일이 다른 연도에 있거나 가능한지 제약중인가요? 1 년에 대한 쿼리? – dazedandconfused

+0

피벗 테이블이 아닙니다. 나는 Jan, Feb 등의 데이터를 가지고 있지 않다. 시작 및 끝 날짜를 기반으로 열을 만들어야합니다. – SharkTiles

답변

1

당신은 기본 case 문에 이런 짓을 했을까 :

select title, start, end, 
     (case when 1 between month(start) and month(end) then 1 else 0 end) as jan, 
     (case when 2 between month(start) and month(end) then 1 else 0 end) as feb, 
     . . . 
     (case when 12 between month(start) and month(end) then 1 else 0 end) as dec 
from table t; 

참고 : 나는 진짜 이름 인 경우 일부 (예약어 및 탈출한다하더라도, 쿼리로 열 이름을 떠납니다).

또한 샘플 데이터에서 첫 번째 표와 두 번째 표 사이에서 날짜가 변경됩니다.

1

날짜를 한 번만 확인하려면이 방법이 유용합니다. 이 샘플을 사용자의 필요에 맞게 적용 할 수 있어야합니다.

SELECT c.CreateDateUTC, DATEPART(MONTH, c.CreateDateUTC) 'MONTH', 
    CASE DATEPART(MONTH, c.CreateDateUTC) 
    WHEN 1 THEN 1 
    END 'JAN', 
    CASE DATEPART(MONTH, c.CreateDateUTC) 
    WHEN 2 THEN 1 
    END 'FEB', 
    CASE DATEPART(MONTH, c.CreateDateUTC) 
    WHEN 3 THEN 1 
    END 'MAR', 
    CASE DATEPART(MONTH, c.CreateDateUTC) 
    WHEN 4 THEN 1 
    END 'APR', 
    CASE DATEPART(MONTH, c.CreateDateUTC) 
    WHEN 5 THEN 1 
    END 'MAY', 
    CASE DATEPART(MONTH, c.CreateDateUTC) 
    WHEN 6 THEN 1 
    END 'JUN', 
    CASE DATEPART(MONTH, c.CreateDateUTC) 
    WHEN 7 THEN 1 
    END 'JUL', 
    CASE DATEPART(MONTH, c.CreateDateUTC) 
    WHEN 8 THEN 1 
    END 'AUG', 
    CASE DATEPART(MONTH, c.CreateDateUTC) 
    WHEN 9 THEN 1 
    END 'SEP', 
    CASE DATEPART(MONTH, c.CreateDateUTC) 
    WHEN 10 THEN 1 
    END 'OCT', 
    CASE DATEPART(MONTH, c.CreateDateUTC) 
    WHEN 11 THEN 1 
    END 'NOV', 
    CASE DATEPART(MONTH, c.CreateDateUTC) 
    WHEN 12 THEN 1 
    END 'DEC' 
FROM dbo.Code c 

는 결과 :

enter image description here

0

당신이 널 (null)를 확인해야합니다, 확장하려면, 당신은 사용할 수는 ISNULL은 (STARTDATE,에 GetDate()는) 오늘 당신을 줄 것이다 그것은 당신의 범위를 맞는 경우 필요합니다.

select *, 
    case when StartDate is not null and EndDate is not null and 1 between MONTH(StartDate) and Month(EndDate) then 1 else 0 end Jan, 
    case when StartDate is not null and EndDate is not null and 2 between MONTH(StartDate) and Month(EndDate) then 1 else 0 end Feb, 
    case when StartDate is not null and EndDate is not null and 3 between MONTH(StartDate) and Month(EndDate) then 1 else 0 end Mar, 
    case when StartDate is not null and EndDate is not null and 4 between MONTH(StartDate) and Month(EndDate) then 1 else 0 end Apr, 
    case when StartDate is not null and EndDate is not null and 5 between MONTH(StartDate) and Month(EndDate) then 1 else 0 end May, 
    case when StartDate is not null and EndDate is not null and 6 between MONTH(StartDate) and Month(EndDate) then 1 else 0 end Jun, 
    case when StartDate is not null and EndDate is not null and 7 between MONTH(StartDate) and Month(EndDate) then 1 else 0 end Jul, 
    case when StartDate is not null and EndDate is not null and 8 between MONTH(StartDate) and Month(EndDate) then 1 else 0 end Aug, 
    case when StartDate is not null and EndDate is not null and 9 between MONTH(StartDate) and Month(EndDate) then 1 else 0 end Sep, 
    case when StartDate is not null and EndDate is not null and 10 between MONTH(StartDate) and Month(EndDate) then 1 else 0 end Oct, 
    case when StartDate is not null and EndDate is not null and 11 between MONTH(StartDate) and Month(EndDate) then 1 else 0 end Nov, 
    case when StartDate is not null and EndDate is not null and 12 between MONTH(StartDate) and Month(EndDate) then 1 else 0 end Dec 
from Foo 
+0

IS NOT NULL 검사는 여기에서 중복됩니다. 값이 NULL 인 날짜 사이에 평가되지 않습니다. –