【问题标题】:Adding additional criteria to a right join向右连接添加附加条件
【发布时间】:2017-01-13 11:04:55
【问题描述】:

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

所以我想在 2016 年的报告中添加“11-01”和“10-01”。有谁知道我该怎么做?

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

【问题讨论】:

  • 你能不能不把12改成10?这样每次都会多做两个月。
  • 抱歉,对此建议的回复较晚。不幸的是,这仅显示 2016-10 日期,而不是 2016-10 & 2016-11 & 2016-12 所以不起作用。

标签: sql sql-server reportbuilder


【解决方案1】:

我会将您的查询改写为

;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

【讨论】:

    猜你喜欢
    • 2012-03-21
    • 1970-01-01
    • 2019-02-02
    • 2019-07-17
    • 1970-01-01
    • 2018-08-02
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    相关资源
    最近更新 更多