如何进行具有不同Where语句的查询

时间:2018-12-18 19:47:40

标签: sql sql-server reporting-services

我与一家保险公司合作,必须创建这样的报告:Excel文档,其中包含应该如何在SSRS上存储数据

我的问题是我无法弄清楚如何计算不同列中的引号,绑定和提交数,因为它们都是从同一字段tbl_Quote.AccountId

中提取的

我基本上需要将这三个其他查询联接在一起,以在结果集中的不同列中拥有各自的计数和总计,我不确定该怎么做。

这是代码:

     --RENEWAL BUSINESS

DECLARE @StartDate DATETIME = '05/1/18'
DECLARE @EndDate DATETIME = '11/30/2018'
DECLARE @Policy int = 33

--SUBMISSIONS
SELECT     Month(S.CoverageEffDate) [Month], count(distinct Q.AccountId) as [R#ofsubmissions],(SUM(S.NotTaxableSubTotal + S.NotTaxableSubTotal2 + S.TaxableSubTotal)) as RSubmittedPremium

FROM         tbl_Quote Q INNER JOIN
                      tbl_Quote_Scheduled_Locations S ON Q.id = S.Ouote_ID
WHERE     (S.CoverageEffDate <= @EndDate) AND (S.CoverageEffDate >= @StartDate) AND (Q.Renewal = 1) AND (Q.Cancellation <> 1) and Q.Endorsement=0 and Q.Policy_ID= @policy
Group By Month(S.CoverageEffDate)
ORDER BY Month;

--QUOTES
SELECT     Month(S.CoverageEffDate) [Month], count(distinct Q.AccountId) as [R#ofquotes], (SUM(S.NotTaxableSubTotal + S.NotTaxableSubTotal2 + S.TaxableSubTotal)) as RQuotedPremium

FROM         tbl_Quote Q INNER JOIN
                      tbl_Quote_Scheduled_Locations S ON Q.id = S.Ouote_ID
WHERE     (S.CoverageEffDate <= @EndDate) AND (S.CoverageEffDate >= @StartDate) AND (Q.Renewal = 1) AND  (Q.Cancellation <> 1)  and Q.Endorsement=0 AND (Q.QuoteStatus_OID = 6 OR Q.QuoteStatus_OID = 23 OR Q.QuoteStatus_OID = 10 OR Q.QuoteStatus_OID =8 ) and Q.Policy_ID= @policy
Group By Month(S.CoverageEffDate)
ORDER BY Month;


--BINDS
SELECT     Month(S.CoverageEffDate) [Month], count(distinct Q.AccountId) as [R#ofbinds], (SUM(S.NotTaxableSubTotal + S.NotTaxableSubTotal2 + S.TaxableSubTotal)) as RBoundPremium

FROM         tbl_Quote Q INNER JOIN
                      tbl_Quote_Scheduled_Locations S ON Q.id = S.Ouote_ID
WHERE     (S.CoverageEffDate <= @EndDate) AND (S.CoverageEffDate >= @StartDate) AND (Q.Renewal = 1) AND  (Q.Cancellation <> 1)  and Q.Endorsement=0 AND (S.Bound = 1 or Q.QuoteStatus_OID = 23) and Q.Policy_ID= @policy
Group By Month(S.CoverageEffDate)
ORDER BY Month;

由于SSRS可以进行总计,因此我需要查询除总计之外还完全匹配excel文档。

我提供的代码仅用于续订。

1 个答案:

答案 0 :(得分:0)

然后使用派生表。

SELECT S.[Month]
    , S.[R#ofsubmissions]
    , S.[RSubmittedTotalTIV]
    , S.RSubmittedPremium
    , Q.[R#ofquotes]
    , Q.[RQuotedTotalTIV]
    , Q.RQuotedPremium
    , B.[R#ofbinds]
    , B.[RBoundTotalTIV]
    , B.RBoundPremium
FROM 
(
    SELECT     
        Month(L.CoverageEffDate) [Month], 
        count(distinct Q.AccountId) as [R#ofsubmissions], 
        SUM(L.TIV) as [RSubmittedTotalTIV],
        (SUM(L.NotTaxableSubTotal + L.NotTaxableSubTotal2 + L.TaxableSubTotal)) as RSubmittedPremium
    FROM tbl_Quote Q
    INNER JOIN tbl_Quote_Scheduled_Locations L
        ON Q.id = L.Ouote_ID
    WHERE (L.CoverageEffDate <= @EndDate) 
        AND (L.CoverageEffDate >= @StartDate) 
        AND (Q.Renewal = 1) AND (Q.Cancellation <> 1) 
        and Q.Endorsement=0 
        and Q.Policy_ID= @policy
    Group By Month(L.CoverageEffDate)
) S
    INNER JOIN 
( 
    SELECT     
        Month(L.CoverageEffDate) [Month], 
        count(distinct Q.AccountId) as [R#ofquotes], 
        SUM(L.TIV) as [RQuotedTotalTIV],
        (SUM(L.NotTaxableSubTotal + L.NotTaxableSubTotal2 + L.TaxableSubTotal)) as RQuotedPremium
    FROM tbl_Quote Q
    INNER JOIN L 
        ON Q.id = L.Ouote_ID
    WHERE (L.CoverageEffDate <= @EndDate) 
        AND (L.CoverageEffDate >= @StartDate) 
        AND (Q.Renewal = 1) 
        AND  (Q.Cancellation <> 1)  
        and Q.Endorsement=0 
        AND (Q.QuoteStatus_OID = 6 OR Q.QuoteStatus_OID = 23 OR Q.QuoteStatus_OID = 10 OR Q.QuoteStatus_OID =8 ) 
        and Q.Policy_ID= @policy
    Group By Month(L.CoverageEffDate)
) Q
        ON S.[Month] = Q.[Month]
    INNER JOIN 
(
    SELECT     
        Month(L.CoverageEffDate) [Month], 
        count(distinct Q.AccountId) as [R#ofbinds], 
        SUM(L.TIV) as [RBoundTotalTIV],
        (SUM(L.NotTaxableSubTotal + L.NotTaxableSubTotal2 + L.TaxableSubTotal)) as RBoundPremium
    FROM tbl_Quote Q
    INNER JOIN tbl_Quote_Scheduled_Locations L
        ON Q.id = L.Ouote_ID
    WHERE (L.CoverageEffDate <= @EndDate) 
        AND (L.CoverageEffDate >= @StartDate) 
        AND (Q.Renewal = 1) 
        AND  (Q.Cancellation <> 1)  
        and Q.Endorsement=0 
        AND (L.Bound = 1 or Q.QuoteStatus_OID = 23) 
        and Q.Policy_ID= @policy
    Group By Month(L.CoverageEffDate)
) B
        ON S.[Month] = B.[Month]
ORDER BY S.[Month]