2016-11-14 22 views
0

一個Policy Number可以有多個不同的ClassCode只有一個Premium總量。在我的SSRS報告中,我希望能夠創建參數ClassCode,並且如果我排除(取消選中)至少其中一個ClassCode,則具有Premium的整個保單號碼不需要計算在內。 可以說,如果我想排除ClassCode 58,那麼1,644.00的Premium不應該在我的報告結果中。如何排除一個價值,並具有該值不被計算在一切

enter image description here

我想在我的查詢AND ClassCode <> 58說,但它只是排除了一行,並保持高級anount。但我的目標是要排除一切,如果它有ClassCode 58

SELECT policynumber, 
     classcode, 
     /* 
     Using ROW_NUMBER() to check if it's the first record in the join and returns the Premium value if so, otherwise it will display 0. 
     The ORDER BY (SELECT 0) is there just because I don't need the row number to be based on any explicit order. 
     */ 
     CASE 
     WHEN ROW_NUMBER() 
       OVER (
        PARTITION BY cte1.policynumber 
        ORDER BY (SELECT 0)) = 1 THEN premium 
     ELSE 0 
     END  AS Premium, 
     c.yearnum TransEffYearNum, 
     c.monthnum TransEffMonthNum 
FROM cte1 
     INNER JOIN cte2 
       ON cte1.policynumber = cte2.policynumber 
     RIGHT JOIN tblcalendar c 
       ON c.yearnum = Year(cte1.policyeffectivedate) 
        AND c.monthnum = Month(cte1.policyeffectivedate) 
WHERE c.yearnum = 2016 
     AND classcode <> 58 
+0

ClassCode屬於哪個表? – DVT

+0

在我的查詢中,ClassCode屬於'cte2'。我只在這裏顯示最終的'SELECT'語句。導致相當長的查詢。 – Oleg

+0

因此,對於您的問題的示例,您想要完全取消策略3,還是隻想將ClassCode 57的Premium升級爲0? – DVT

回答

0

你首先應該排除所有政策號碼有,你要設置的ClassCode。 所以不是 where c.YearNum = 2016 AND ClassCode <> 58

使用

where PolicyNumber NOT IN 
(SELECT PolicyNumber 
From cte1 inner join cte2 on cte1.PolicyNumber=cte2.PolicyNumber 
WHERE ClassCode = 58); 

而且應該這樣做。

0

如果你想消除策略3的所有行,這可能會起作用。

SELECT policynumber, 
     classcode, 
     /* 
     Using ROW_NUMBER() to check if it's the first record in the join and returns the Premium value if so, otherwise it will display 0. 
     The ORDER BY (SELECT 0) is there just because I don't need the row number to be based on any explicit order. 
     */ 
     CASE 
     WHEN ROW_NUMBER() 
       OVER (
        PARTITION BY cte1.policynumber 
        ORDER BY (SELECT 0)) = 1 THEN premium 
     ELSE 0 
     END  AS Premium, 
     c.yearnum TransEffYearNum, 
     c.monthnum TransEffMonthNum 
FROM cte1 
     INNER JOIN cte2 
       ON cte1.policynumber = cte2.policynumber 
     RIGHT JOIN tblcalendar c 
       ON c.yearnum = Year(cte1.policyeffectivedate) 
        AND c.monthnum = Month(cte1.policyeffectivedate) 
WHERE c.yearnum = 2016 
     AND cte2.policynumber NOT IN (SELECT policynumber FROM cte2 WHERE ClassCode = 58 and policynumber IS NOT NULL); 
+0

謝謝。看起來像正確的邏輯,但我的查詢永遠旋轉。因爲某些原因。 – Oleg

+0

我想你的cte?可能是巨大的。但那是另一個問題。你可能想擺脫cte2中的policynumber = 3。 – DVT

相關問題