2017-01-13 1 views
0

이것은이 포럼에 대한 첫 번째 게시물이며 SQL에 대해 매우 익숙하므로 나와 함께하시기 바랍니다.오른쪽 조인에 추가 기준 추가

일부 기존 스크립트를 수정하여 보고서를 약간 수정하여 목적에 맞게 약간 수정했습니다 (원본은 개발자가 함께 작성했습니다).

이 보고서는 매월 세 개의 항목에 대해 두 개의 특정 파일이 데이터베이스에 읽혔는지 또는 누락되었는지 여부를 확인하려고합니다.

출력 외모

는 아래 추천했습니다 :

File A 
YYYY:MM:DD A MISSING 
      B MISSING 
      C MISSING 

YYYY:MM:DD A Present 
      B MISSING 
      C Present 

스크립트는 현재 연도 파일이 전년 12 월의 파일을 제외 찾았다되도록이다, 그러나 나는 또한 원하는 전년도의 10 월과 11 월 결과를 표시합니다.

다음은 관련 스크립트의 일부입니다

select distinct(k.filedate) as filedate, k.fid, case when r.fundid is null then 0 else 1 end as present  
from XXXX database  
right join  
(  
    select convert(date,convert(varchar, year(@ReportDate) - 1) + '-12-01') as filedate, g.fid 
    from ( 
     select 'XXXXFDGBP10' as fid 
     union 
     select 'XXXXUSD10' as fid 
     union 
     select 'XXXXUSD10' as fid 
     union 
     select 'XXXXA10' as fid 
     union 
     select 'XXXXB10' as fid 
     union 
     select 'XXXXGBPMGMT10' as fid 
     union 
     select 'XXXXMGMTSH10' as fid 
    ) g 

    union 

    select convert(date,convert(varchar, year(@ReportDate)) + '-' + convert(varchar, h.m) + '-01') as filedate, s.fid 
    from ( 
     select 'XXXXFDGBP10' as fid 
     union 
     select 'XXXXUSD10' as fid 
     union 
     select 'XXXXUSD10' as fid 
     union 
     select 'XXXXA10' as fid 
     union 
     select 'XXXXB10' as fid 
     union 
     select 'XXXXGBPMGMT10' as fid 
     union 
     select 'XXXXMGMTSH10' as fid 
    ) s,  
    ( 
     select 1 as m 
     union 
     select 2 as m 
     union 
     select 3 as m 
     union 
     select 4 as m 
     union 
     select 5 as m 
     union 
     select 6 as m 
     union 
     select 7 as m 
     union 
     select 8 as m 
     union 
     select 9 as m 
     union 
     select 10 as m 
     union 
     select 11 as m 
     union 
     select 12 as m 
    ) h 
) k  
on r.fundid = k.fid and r.filedate = k.filedate  
where  
    k.filedate >= convert(date,convert(varchar, year(@ReportDate) - 1) + '-12-01') 
    and k.filedate <= @ReportDate 

그래서 내가 '11를 추가 할 -01 '및 '10 -01'보고서의 2016 년으로 돌아갑니다. 아무도 내가 이것을 어떻게 할 수 있는지 안다?

미리 감사드립니다. 이것이 명확하지 않거나 질문이있는 경우 알려주십시오.

+0

같은 쿼리를 다시 작성할 것? 그렇게하면 매회 두 달이 더 걸릴 것입니다. – BishNaboB

+0

이 제안에 대한 늦은 답변에 사과드립니다. 불행히도 이것은 단지 2016-10 및 2016-11 & 2016-12가 아닌 2016-10 날짜를 보여줍니다. – idontknowwhatiamdoing

답변

0

난 당신이뿐만 아니라 12 ~ 10을 변경할 수

;WITH months AS 
(
    SELECT * FROM (VALUES (1), (2), (3), (4), (5), (6), (7), (8), (9), (10), (11), (12)) AS f("month") 
), 
years AS 
(
    SELECT * FROM (VALUES (year(@ReportDate)), (year(@ReportDate)-1)) AS f("year") 
), 
fids AS 
(
    SELECT * FROM (VALUES ('XXXXFDGBP10'), ('XXXXUSD10'), ('XXXXUSD10'), ('XXXXA10'), ('XXXXB10'), ('XXXXGBPMGMT10'), ('XXXXMGMTSH10')) AS f(fid) 
), 
k AS 
(
    SELECT 
     filedate = DATEADD(month, [month]-1, DATEADD(year, [year]-1900, 0)), 
     fid 
    FROM fids 
    CROSS JOIN years 
    CROSS JOIN months 
) 
SELECT 
    k.filedate, 
    k.fid, 
    present = case when r.fundid is null then 0 else 1 end  
FROM XXXX r 
RIGHT JOIN k ON r.fundid = k.fid and r.filedate = k.filedate 
where  
    k.filedate >= convert(date,convert(varchar, year(@ReportDate) - 1) + '-10-01') 
    and k.filedate <= @ReportDate