尝试使用CASE
作为条件参数
我之前已经发布了此内容;但是由于无法获得正确的结果,请使用其他方法再次发布。
我有一个复杂的代码,只添加了一些用黄色突出显示的元素
我的目标是:
当@Depreciation = 0
需要显示值-不包含“折旧”(其中DepreciationAcc = 0)
@Depreciation = 1时
需要显示值-使用“折旧”(其中DepreciationAcc均为0或1)
可以,但是选择的行数不正确。
这是我在WHERE中使用CASE的部分:
WHERE (
(CASE WHEN (NOT (acct.[GLAccountCode] IN (N'9040',N'9047',N'4130')) AND
(@Depreciation = 0))THEN 0 ELSE trans.Amount END)=0 .............
下面是完整代码。我正在尝试修改的部分-标记为/ **** / :(主要在第一个WHERE之后)
DECLARE @FYMonthBOP date = '2017-07-01'
DECLARE @FYMonthEOP date = '2018-03-31'
DECLARE @GLProgramGroupsWHID int = -1
DECLARE @GLProgramsWHID_Groups int = -1
DECLARE @GLProgramGroupSetsWHID int = -1
DECLARE @Depreciation bit = 1 /****/
SELECT
gl0.GLGroupLevel0Name
, gl0.Ordinal AS Ordinal0
, [Dimension].GLGroupLevel1.GLGroupLevel1Name
, [Dimension].GLGroupLevel1.Ordinal AS Ordinal1
, gl2.GLGroupLevel2Name
, gl2.Ordinal AS Ordinal2
, acct.GLAccountCode
, acct.GLAccountName
, pgm.GLProgramCode + N' ' + pgm.GLProgramName AS Program
, setname.[WHID] AS [WHIDGroupSetName]
, grp.GroupName
, grp.WHID AS WHIDGroupName
, grp.Ordinal AS OrdinalGroupName
, d.FYQuarterLabel AS FQ
-- , trans.Amount AS Amount
-- , CASE WHEN ((acct.[GLAccountCode] IN (N'9040',N'9047',N'4130'))
AND (@Depreciation = 0)) THEN 0 ELSE trans.Amount END AS [Amount]
, pgm.[WHID] AS WHIDProgram
, pgm.Ordinal AS OrdinalProgram
, d.FYMonthNumber
, d.FYMonthShortLabel
, d.FYQuarterNumber
, d.FYNumber
, d.FYLabel
, d.YearMonthSort
, IIF(EOMonth(mm.DateThru) < @FYMonthEOP, EOMonth(mm.DateThru),
@FYMonthEOP) AS [DateThru]
, IIF((acct.[GLAccountCode]) IN (N'9040',N'9047',N'4130'), 1, 0) AS
[DepreciationAcct]
FROM
(([Dimension].GLGroupLevel2 gl2 WITH (NoLock)
INNER JOIN [Dimension].GLAccount acct WITH (NoLock) ON gl2.[WHID] =
acct.GLGroupLevel2FK
INNER JOIN [Dimension].GLGroupLevel0 gl0 WITH (NoLock)
INNER JOIN [Dimension].GLGroupLevel1 WITH (NoLock) ON gl0.[WHID] =
[Dimension].GLGroupLevel1.GLGroupLevel0FK ON
GLGroupLevel1FK = [Dimension].GLGroupLevel1.[WHID]
INNER JOIN Fact.GLTransactionsMonthly trans WITH (NoLock) ON acct.
[WHID] = trans.GLAccountFK
INNER JOIN [Dimension].GLProgram pgm WITH (NoLock) ON trans.
[GLProgramFK] = pgm.[WHID] AND trans.[GLProgramFK] =
pgm.[WHID] AND trans.[GLProgramFK] = pgm.[WHID]
INNER JOIN [Dimension].[Date] d WITH (NoLock) ON trans.EffectiveMonth =
d.[Date] AND trans.EffectiveMonth = d.[Date]
AND trans.EffectiveMonth = d.[Date]
INNER JOIN [Reports].GLProgramGroupMapping map ON map.GLProgramFK =
pgm.[WHID]
INNER JOIN [Reports].GLProgramGroup grp ON grp.WHID =
map.GLProgramGroupFK
INNER JOIN [Reports].GLProgramGroupSet setname ON setname.WHID =
grp.GLProgramGroupSetFK))
CROSS APPLY (
SELECT MAX(t.EffectiveMonth) AS [DateThru]
FROM Fact.GLTransactionsMonthly t WITH (NoLock)
) mm
WHERE (
(CASE WHEN (NOT (acct.[GLAccountCode] IN (N'9040',N'9047',N'4130'))
AND (@Depreciation = 0))
THEN 0 ELSE trans.Amount END)=0 AND /****/
(trans.EffectiveMonth BETWEEN @FYMonthBOP AND @FYMonthEOP)
AND ((setname.WHID IN (@GLProgramGroupSetsWHID)) OR (-1 IN
(@GLProgramGroupSetsWHID)))
AND ((grp.WHID IN (@GLProgramGroupsWHID)) OR (-1 IN
(@GLProgramGroupsWHID)))
AND ((pgm.[WHID] IN (@GLProgramsWHID_Groups)) OR (-1 IN
(@GLProgramsWHID_Groups)))
AND (
(pgm.[WHID] IN (
SELECT DISTINCT
f.[GLProgramFK]
FROM
Fact.GLTransactionsMonthly f
INNER JOIN [Dimension].[GLAccount] acct ON f.
[GLAccountFK] = acct.[WHID]
WHERE
(f.EffectiveMonth BETWEEN @FYMonthBOP AND @FYMonthEOP)
AND (acct.[GLAccountCode] BETWEEN '5000' AND '9999')
GROUP BY
f.[GLProgramFK]
HAVING
(SUM(f.[Amount]) <> 0))
)
OR
(pgm.[WHID] IN (
SELECT DISTINCT
f.[GLProgramFK]
FROM
Fact.GLTransactionsMonthly f
INNER JOIN [Dimension].[GLAccount] acct ON f.
[GLAccountFK] = acct.[WHID]
WHERE
(f.EffectiveMonth BETWEEN @FYMonthBOP AND @FYMonthEOP)
AND (acct.[GLAccountCode] BETWEEN '4000' AND '4999')
GROUP BY
f.[GLProgramFK]
HAVING
(SUM(f.[Amount]) <> 0))
)
OR
(pgm.[GLProgramCode] IN ('549'))
)
)
答案 0 :(得分:0)
考虑谓词的基本结构:
(CASE
WHEN ... AND @Depreciation = 0 THEN 0
ELSE trans.Amount END
) = 0
案例表达式中出现的 所有均用等于0 进行求值,因此如果trans.Amount <> 0
则它将无法生存到结果中
编辑
我想你可能想要这个:
WHERE (
(@Depreciation = 0 and [DepreciationAcct] = 0)
OR
(@Depreciation = 1 and [DepreciationAcct] IN (0,1))
)
答案 1 :(得分:0)
最近才了解解决此问题的正确方法-
CASE WHEN (acct.[GLAccountCode] IN (N'9040',N'9047',N'4130')) AND
(@Depreciation = 0) THEN 0 ELSE 1 END)=1
无需包含trans.Amount字段,该值必须为等于1 ,而不是0 这样,它将显示所有值