向右连接添加其他条件

时间:2017-01-13 11:04:55

标签: sql sql-server reportbuilder

这是我在这个论坛的第一篇文章,我对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   

所以我想添加&#39; 11-01&#39;和&#39; 10-01&#39;到2016年报告的回归。有谁知道我怎么能这样做?

提前致谢。如果不清楚或者是否有人有任何问题,请告诉我。

1 个答案:

答案 0 :(得分:0)

我将您的查询重写为

;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