使用Case语句根据单独字段中的值替换一个字段中的值

时间:2014-04-03 22:50:43

标签: sql reporting-services replace case iif

我正在SSRS中创建报告,我无法在报告中使用IIF语句来完成以下操作。当我尝试在SSRS报告中求和时,我收到了汇总错误。

`IIF(Fields!Period=0,0,IIF(Period=13,0,Balance/12))`

直到我尝试Sum的那一刻工作正常..得到一个愚蠢的聚合错误“First,Last,Previous,Count和Count Distinct以外的聚合函数只能聚合单个数据类型的数据”......这些都是整数。

基本上我在Master.Balance中有一个值,我只需要在Secondary.Period等于0或13时除以12.如果Secondary.Period等于0或13,那么该值应为0.我知道我的问题必须包括表之间的关系,但我只是不知道如何写它。

以下是我正在尝试使用的内容:

`CASE 
     WHEN Secondary.Period=0 OR Secondary.Period=13 
         THEN 0 
         ELSE Master.Balance/12 
End As BudByPer`

这两个表是如何相互关联的:

`FROM Master LEFT OUTER JOIN Secondary 

ON Master.Project = Secondary.Project 
AND Master.object = Secondary.object 
AND Master.org = Secondary.org

`

如何将上述内容纳入其中:

    SELECT DISTINCT Master.Project, Master.Object, Master.Fund, Master.Segment, Master.Balance, Secondary.project, Secondary.object, Secondary.org, Secondary.Period, Secondary.object, Secondary.Project.

    FROM Master LEFT OUTER JOIN Secondary 
        ON Master.Project = Secondary.Project 
        AND Master.object = Secondary.object 
        AND Master.org = Secondary.org

WHERE (Master.object>=600000)
ORDER BY [Master.Fund]

1 个答案:

答案 0 :(得分:0)

你只需要一个选择,它对我来说很好......

SELECT 
    Master.account, 
    Master.segment,  
    Secondary.desc, 
    Secondary.bud, 
    Segment.Num, 
    Segment.office,
    CASE 
        WHEN Secondary.Period=0 OR Secondary.Period=13 THEN 0 
        ELSE Master.Balance/12 
    End As BudByPer
FROM Master 
LEFT JOIN Secondary 
    ON Master.Project = Secondary.Project 
    AND Master.object = Secondary.object 
    AND Master.org = Secondary.org