通过MTD,上个月等将查询限制为SSRS

时间:2013-09-30 19:10:48

标签: sql reporting-services reporting

我正在尝试从以下查询中生成一个报告,我遇到的问题是:我如何让它构建一个我可以在SSRS中调用它的结构,比如称MTF,PMonth,PPMonth为“类型”选择,当我把它放入SSRS时,我希望MTD,PMonth和PPMonth作为单独的实体出现在报告中。

-- ResWare.dbo.rptClosingPipeline3
SET NOCOUNT ON

SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED

DECLARE @today varchar(20)
SET     @today = Convert(varchar, GetDate(), 101)

DECLARE @startdate  datetime
SET @startdate = dbo.GetUTCDateTimeFunc(DateAdd(m, 0, convert(datetime, convert(char(8), current_timestamp, 112)) - day(current_timestamp) + 1))--

DECLARE @enddate    datetime  --today at midnight.  first moment of the day
SET @enddate = dbo.GetUTCDateTimeFunc(@today)--

DECLARE @MTD    datetime
SET @MTD = dbo.GetUTCDateTimeFunc(DateAdd(m, 0, convert(datetime, convert(char(8), current_timestamp, 112)) - day(current_timestamp) + 1))--

DECLARE @PMonth datetime
SET @PMonth = dbo.GetUTCDateTimeFunc(DateAdd(m, -1, convert(datetime, convert(char(8), current_timestamp, 112)) - day(current_timestamp) + 1))
DECLARE @PMonthEnd  datetime
SET @PMonthEnd = dbo.GetUTCDateTimeFunc(DateAdd(m, 0, convert(datetime, convert(char(8), current_timestamp, 112)) - day(current_timestamp) + 1))--

DECLARE @PPMonth    datetime
SET @PPMonth = dbo.GetUTCDateTimeFunc(DateAdd(m, -2, convert(datetime, convert(char(8), current_timestamp, 112)) - day(current_timestamp) + 1))
DECLARE @PPMonthEnd datetime
SET @PPMonthEnd = dbo.GetUTCDateTimeFunc(DateAdd(m, -1, convert(datetime, convert(char(8), current_timestamp, 112)) - day(current_timestamp) + 1))--


SELECT  FM.FileNumber AS FileNumber,
        ISNULL(IU.TEAMNAME,'') AS Closer,
        dbo.GetLocalDateTimeFunc(FM.ActualSettlementDate) AS Scheduled,
        dbo.GetLocalDateTimeFunc(FM.DisbursementDate) AS DisbursementDate,
        dbo.GetLocalDateTimeFunc(FM.EstSettlementDate) AS EstSettlementDate,
        CASE WHEN EXISTS (SELECT * FROM FilePartnerRel FPR1
        WHERE FPR1.PartnerTypeID = 10033 AND FPR1.FileID = FM.FileID)
        THEN P.State+'-(W)'
        ELSE P.State END AS State,
        ISNULL(P.StreetNumber,'')+' '+ISNULL(P.StreetDirection,'')+' '+ISNULL(P.StreetName,'')+' '+ISNULL(P.Unit,'')AS PropertyAddress,
        S.Name,
        (SELECT CONVERT(varchar(7000),Body) FROM Note WHERE NoteID = 
        (Select MAX(N.NoteID)
         FROM Note N
         JOIN FileActions A1 ON A1.FileID=FM.FileID 
         AND A1.ActionDefID IN (141,386,306)
        JOIN FileActionNoteRel AN ON AN.FileActionsID=A1.FileActionsID
         AND N.NoteID = AN.NoteID)) AS Note,
         (SELECT COUNT(DISTINCT FA1.FileID) FROM FileActions FA1
         JOIN FileMain FM1 ON FM1.FileID = FA1.FileID
              AND FM1.FileNumber NOT LIKE 'PA-%'
        JOIN    Coordinator C1 ON C1.FileID = FA1.FileID
        AND C1.CoordinatorTypeID IN (2, 6)
        AND C1.CoordinatorID = C.CoordinatorID
        WHERE FA1.ActionDefID IN (61,315,393)
        AND FA1.ReceivedDate >= @startdate
        AND FM1.StatusID != 8),
         (SELECT COUNT(DISTINCT FA1.FileID) FROM FileActions FA1
         JOIN FileMain FM1 ON FM1.FileID = FA1.FileID
              AND FM1.FileNumber NOT LIKE 'PA-%'
        WHERE FA1.ActionDefID IN (61,315,393)
        AND FA1.ReceivedDate >= @startdate 
        AND FM1.StatusID != 8) AS TotalDisb
        , TRT.Name CurativeItem
        , SD.CommitmentEffDate
FROM        FileMain FM
JOIN        ActionListTransactionType ALTT ON ALTT.ActionListTransactionTypeID = FM.ActionListTransactionTypeID
JOIN        ActionListProductType ALPT ON ALPT.ActionListProductTypeID = FM.ActionListProductTypeID
JOIN        Coordinator C ON C.FileID = FM.FileID
            AND C.CoordinatorTypeID IN (2)
LEFT JOIN   FilePartnerRel FPR ON FPR.FileID = FM.FileID
            AND FPR.PartnerTypeID = 5
JOIN        Property P ON P.FileID = FM.FileID
LEFT JOIN   PartnerCompany PC ON PC.PartnerCompanyID = FPR.PartnerCompanyID
JOIN        PartnerCompany PC1 ON PC1.PartnerCompanyID = FM.ClientID
JOIN        TEAM IU ON IU.TEAMID = C.CoordinatorID
JOIN        Status S ON S.StatusID = FM.StatusID
LEFT JOIN   TitleReview TR ON TR.FileID = Fm.FileID
            AND (TR.InternalClear IS NULL OR TR.InternalClear = 0)
LEFT JOIN   TitleReviewType TRT ON TRT.TitleReviewTypeID = TR.TitleReviewTypeID
LEFT JOIN   SearchData SD ON SD.FileID = FM.FileID

WHERE   FM.OfficeID = 1
        AND FM.FileNumber NOT LIKE 'PA-%'
        AND FM.StatusID IN (2,4,5,10,11,14,15,18) 

UNION ALL

SELECT  FM.FileNumber AS FileNumber,
        ISNULL(IU.TEAMNAME,'') AS Closer,
        dbo.GetLocalDateTimeFunc(FM.ActualSettlementDate) AS Scheduled,
        dbo.GetLocalDateTimeFunc(FM.DisbursementDate) AS DisbursementDate,
        dbo.GetLocalDateTimeFunc(FM.EstSettlementDate) AS EstSettlementDate,
        CASE WHEN EXISTS (SELECT * FROM FilePartnerRel FPR1
        WHERE FPR1.PartnerTypeID = 10033 AND FPR1.FileID = FM.FileID)
        THEN P.State+'-(W)'
        ELSE P.State END AS State,
        ISNULL(P.StreetNumber,'')+' '+ISNULL(P.StreetDirection,'')+' '+ISNULL(P.StreetName,'')+' '+ISNULL(P.Unit,'')AS PropertyAddress,
        S.Name,
        (SELECT CONVERT(varchar(7000),Body) FROM Note WHERE NoteID = 
        (Select MAX(N.NoteID)
         FROM Note N
         JOIN FileActions A1 ON A1.FileID=FM.FileID 
         AND A1.ActionDefID IN (141,386,306)
        JOIN FileActionNoteRel AN ON AN.FileActionsID=A1.FileActionsID
         AND N.NoteID = AN.NoteID)) AS Note,
         (SELECT COUNT(DISTINCT FA1.FileID) FROM FileActions FA1
         JOIN FileMain FM1 ON FM1.FileID = FA1.FileID
              AND FM1.FileNumber NOT LIKE 'PA-%'
        JOIN    Coordinator C1 ON C1.FileID = FA1.FileID
        AND C1.CoordinatorTypeID IN (2, 6)
        AND C1.CoordinatorID = C.CoordinatorID
        WHERE FA1.ActionDefID IN (61,315,393)
        AND FA1.ReceivedDate >= @MTD
        AND FM1.StatusID != 8),
         (SELECT COUNT(DISTINCT FA1.FileID) FROM FileActions FA1
         JOIN FileMain FM1 ON FM1.FileID = FA1.FileID
              AND FM1.FileNumber NOT LIKE 'PA-%'
        WHERE FA1.ActionDefID IN (61,315,393)
        AND FA1.ReceivedDate >= @MTD 
        AND FM1.StatusID != 8) AS TotalDisb
        , TRT.Name CurativeItem
        , SD.CommitmentEffDate
FROM        FileMain FM
JOIN        ActionListTransactionType ALTT ON ALTT.ActionListTransactionTypeID = FM.ActionListTransactionTypeID
JOIN        ActionListProductType ALPT ON ALPT.ActionListProductTypeID = FM.ActionListProductTypeID
JOIN        Coordinator C ON C.FileID = FM.FileID
            AND C.CoordinatorTypeID IN (2)
LEFT JOIN   FilePartnerRel FPR ON FPR.FileID = FM.FileID
            AND FPR.PartnerTypeID = 5
JOIN        Property P ON P.FileID = FM.FileID
LEFT JOIN   PartnerCompany PC ON PC.PartnerCompanyID = FPR.PartnerCompanyID
JOIN        PartnerCompany PC1 ON PC1.PartnerCompanyID = FM.ClientID
JOIN        TEAM IU ON IU.TEAMID = C.CoordinatorID
JOIN        Status S ON S.StatusID = FM.StatusID
LEFT JOIN   TitleReview TR ON TR.FileID = Fm.FileID
            AND (TR.InternalClear IS NULL OR TR.InternalClear = 0)
LEFT JOIN   TitleReviewType TRT ON TRT.TitleReviewTypeID = TR.TitleReviewTypeID
LEFT JOIN   SearchData SD ON SD.FileID = FM.FileID

WHERE   FM.OfficeID = 1
        AND FM.FileNumber NOT LIKE 'PA-%'
        AND FM.StatusID IN (2,4,5,10,11,14,15,18) 

UNION ALL

SELECT  FM.FileNumber AS FileNumber,
        ISNULL(IU.TEAMNAME,'') AS Closer,
        dbo.GetLocalDateTimeFunc(FM.ActualSettlementDate) AS Scheduled,
        dbo.GetLocalDateTimeFunc(FM.DisbursementDate) AS DisbursementDate,
        dbo.GetLocalDateTimeFunc(FM.EstSettlementDate) AS EstSettlementDate,
        CASE WHEN EXISTS (SELECT * FROM FilePartnerRel FPR1
        WHERE FPR1.PartnerTypeID = 10033 AND FPR1.FileID = FM.FileID)
        THEN P.State+'-(W)'
        ELSE P.State END AS State,
        ISNULL(P.StreetNumber,'')+' '+ISNULL(P.StreetDirection,'')+' '+ISNULL(P.StreetName,'')+' '+ISNULL(P.Unit,'')AS PropertyAddress,
        S.Name,
        (SELECT CONVERT(varchar(7000),Body) FROM Note WHERE NoteID = 
        (Select MAX(N.NoteID)
         FROM Note N
         JOIN FileActions A1 ON A1.FileID=FM.FileID 
         AND A1.ActionDefID IN (141,386,306)
        JOIN FileActionNoteRel AN ON AN.FileActionsID=A1.FileActionsID
         AND N.NoteID = AN.NoteID)) AS Note,
         (SELECT COUNT(DISTINCT FA1.FileID) FROM FileActions FA1
         JOIN FileMain FM1 ON FM1.FileID = FA1.FileID
              AND FM1.FileNumber NOT LIKE 'PA-%'
        JOIN    Coordinator C1 ON C1.FileID = FA1.FileID
        AND C1.CoordinatorTypeID IN (2, 6)
        AND C1.CoordinatorID = C.CoordinatorID
        WHERE FA1.ActionDefID IN (61,315,393)
        AND FA1.ReceivedDate >= @PMonth
        AND FM1.StatusID != 8),
         (SELECT COUNT(DISTINCT FA1.FileID) FROM FileActions FA1
         JOIN FileMain FM1 ON FM1.FileID = FA1.FileID
              AND FM1.FileNumber NOT LIKE 'PA-%'
        WHERE FA1.ActionDefID IN (61,315,393)
        AND FA1.ReceivedDate >= @PMonth 
        AND FM1.StatusID != 8) AS TotalDisb
        , TRT.Name CurativeItem
        , SD.CommitmentEffDate
FROM        FileMain FM
JOIN        ActionListTransactionType ALTT ON ALTT.ActionListTransactionTypeID = FM.ActionListTransactionTypeID
JOIN        ActionListProductType ALPT ON ALPT.ActionListProductTypeID = FM.ActionListProductTypeID
JOIN        Coordinator C ON C.FileID = FM.FileID
            AND C.CoordinatorTypeID IN (2)
LEFT JOIN   FilePartnerRel FPR ON FPR.FileID = FM.FileID
            AND FPR.PartnerTypeID = 5
JOIN        Property P ON P.FileID = FM.FileID
LEFT JOIN   PartnerCompany PC ON PC.PartnerCompanyID = FPR.PartnerCompanyID
JOIN        PartnerCompany PC1 ON PC1.PartnerCompanyID = FM.ClientID
JOIN        TEAM IU ON IU.TEAMID = C.CoordinatorID
JOIN        Status S ON S.StatusID = FM.StatusID
LEFT JOIN   TitleReview TR ON TR.FileID = Fm.FileID
            AND (TR.InternalClear IS NULL OR TR.InternalClear = 0)
LEFT JOIN   TitleReviewType TRT ON TRT.TitleReviewTypeID = TR.TitleReviewTypeID
LEFT JOIN   SearchData SD ON SD.FileID = FM.FileID

WHERE   FM.OfficeID = 1
        AND FM.FileNumber NOT LIKE 'PA-%'
        AND FM.StatusID IN (2,4,5,10,11,14,15,18) 

UNION ALL

SELECT  FM.FileNumber AS FileNumber,
        ISNULL(IU.TEAMNAME,'') AS Closer,
        dbo.GetLocalDateTimeFunc(FM.ActualSettlementDate) AS Scheduled,
        dbo.GetLocalDateTimeFunc(FM.DisbursementDate) AS DisbursementDate,
        dbo.GetLocalDateTimeFunc(FM.EstSettlementDate) AS EstSettlementDate,
        CASE WHEN EXISTS (SELECT * FROM FilePartnerRel FPR1
        WHERE FPR1.PartnerTypeID = 10033 AND FPR1.FileID = FM.FileID)
        THEN P.State+'-(W)'
        ELSE P.State END AS State,
        ISNULL(P.StreetNumber,'')+' '+ISNULL(P.StreetDirection,'')+' '+ISNULL(P.StreetName,'')+' '+ISNULL(P.Unit,'')AS PropertyAddress,
        S.Name,
        (SELECT CONVERT(varchar(7000),Body) FROM Note WHERE NoteID = 
        (Select MAX(N.NoteID)
         FROM Note N
         JOIN FileActions A1 ON A1.FileID=FM.FileID 
         AND A1.ActionDefID IN (141,386,306)
        JOIN FileActionNoteRel AN ON AN.FileActionsID=A1.FileActionsID
         AND N.NoteID = AN.NoteID)) AS Note,
         (SELECT COUNT(DISTINCT FA1.FileID) FROM FileActions FA1
         JOIN FileMain FM1 ON FM1.FileID = FA1.FileID
              AND FM1.FileNumber NOT LIKE 'PA-%'
        JOIN    Coordinator C1 ON C1.FileID = FA1.FileID
        AND C1.CoordinatorTypeID IN (2, 6)
        AND C1.CoordinatorID = C.CoordinatorID
        WHERE FA1.ActionDefID IN (61,315,393)
        AND FA1.ReceivedDate >= @PPMonth
        AND FM1.StatusID != 8),
         (SELECT COUNT(DISTINCT FA1.FileID) FROM FileActions FA1
         JOIN FileMain FM1 ON FM1.FileID = FA1.FileID
              AND FM1.FileNumber NOT LIKE 'PA-%'
        WHERE FA1.ActionDefID IN (61,315,393)
        AND FA1.ReceivedDate >= @PPMonth 
        AND FM1.StatusID != 8) AS TotalDisb
        , TRT.Name CurativeItem
        , SD.CommitmentEffDate
FROM        FileMain FM
JOIN        ActionListTransactionType ALTT ON ALTT.ActionListTransactionTypeID = FM.ActionListTransactionTypeID
JOIN        ActionListProductType ALPT ON ALPT.ActionListProductTypeID = FM.ActionListProductTypeID
JOIN        Coordinator C ON C.FileID = FM.FileID
            AND C.CoordinatorTypeID IN (2)
LEFT JOIN   FilePartnerRel FPR ON FPR.FileID = FM.FileID
            AND FPR.PartnerTypeID = 5
JOIN        Property P ON P.FileID = FM.FileID
LEFT JOIN   PartnerCompany PC ON PC.PartnerCompanyID = FPR.PartnerCompanyID
JOIN        PartnerCompany PC1 ON PC1.PartnerCompanyID = FM.ClientID
JOIN        TEAM IU ON IU.TEAMID = C.CoordinatorID
JOIN        Status S ON S.StatusID = FM.StatusID
LEFT JOIN   TitleReview TR ON TR.FileID = Fm.FileID
            AND (TR.InternalClear IS NULL OR TR.InternalClear = 0)
LEFT JOIN   TitleReviewType TRT ON TRT.TitleReviewTypeID = TR.TitleReviewTypeID
LEFT JOIN   SearchData SD ON SD.FileID = FM.FileID

WHERE   FM.OfficeID = 1
        AND FM.FileNumber NOT LIKE 'PA-%'
        AND FM.StatusID IN (2,4,5,10,11,14,15,18) 


GROUP BY        FM.FileNumber,
        PC1.Name,
        PC.Name,
        ISNULL(IU.TEAMNAME,''),
        ALTT.Name +': '+ALPT.DisplayName,
        dbo.GetLocalDateTimeFunc(FM.ActualSettlementDate),
        dbo.GetLocalDateTimeFunc(FM.DisbursementDate),
        dbo.GetLocalDateTimeFunc(FM.EstSettlementDate),
        FM.FileID,
        P.State,
        P.StreetNumber,
        P.StreetDirection,
        P.StreetName,
        P.Unit,
        S.Name,
        FM.EstSettlementDate,
        C.CoordinatorID,
        TRT.Name,
        SD.CommitmentEffDate

1 个答案:

答案 0 :(得分:0)

选项1:

在查询中添加硬编码列,如下所示:

SELECT ...
, 'StartDt' as Period
From QUERY1
UNION ALL
SELECT ...
, 'MTD' as Period
FROM QUERY2
UNION ALL
SELECT ...
, 'Pmonth' as Period
FROM QUERY3

然后我会添加一个报告参数" @ filter" (或者您喜欢的任何名称)使用" StartDt' MTD' Pmonth'等作为其可用值。启用多个值并确保它是文本参数。

然后设置报告过滤器的每个部分,以查看"期间" column匹配过滤器的parameter.value。

选项2:

如上所述添加报告参数@filter。 创建一个表变量,并将filter参数传入的值插入其中。

使用案例何时将1或0传递回最后一列中的SSRS,表示该部分是否应该可见。

,Period = CASE
WHEN 'StartDt' IN (SELECT * FROM @table) then 1
ELSE 0
END

然后将过滤器设置为仅包含周期值为1的数据,并相应地设置报告部分的可见性。