我有一个现有代码,必须在其中添加新参数- @折旧为“是”或“否”
如果@Depreciation = 1(我认为它对应于“是”) 那么我希望现有代码的确定条件是 否(我在代码段中用'-***'标记),
反之亦然-
如果@Depreciation = 0(“否”) 然后我希望代码中的条件是 是
条件是
(NOT (acct.[GLAccountCode] IN (N'9040',N'9047',N'4130')))
在下面的代码中位于第一个WHERE之后。
请不要对整个代码过于关注。 我唯一需要修改的部分是- ' DECLARE @Depreciation ....” 和第一个'WHERE .....'之后-我标记了-***
DECLARE @FYMonthBOP date = '07-01-2016'
DECLARE @FYMonthEOP date = '07-01-2018'
DECLARE @GLProgramGroupsWHID int = -1
DECLARE @GLProgramsWHID_Groups int = -1
DECLARE @GLProgramGroupSetsWHID int = -1
DECLARE @Depreciation bit = 1 -- *** This is my added Parameter
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
, setname.WHID AS WHIDGroupSetName
, setname.GroupSetName
, pgm.GLProgramCode + N' ' + pgm.GLProgramName AS Program
, grp.GroupName
, grp.WHID AS WHIDGroupName
, grp.Ordinal AS OrdinalGroupName
, d.FYQuarterLabel AS FQ
, trans.Amount 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]
FROM
([Dimension].GLGroupLevel2 gl2
INNER JOIN [Dimension].GLAccount acct ON gl2.[WHID] =
acct.GLGroupLevel2FK
INNER JOIN [Dimension].GLGroupLevel0 gl0
INNER JOIN [Dimension].GLGroupLevel1 ON gl0.[WHID] =
[Dimension].GLGroupLevel1.GLGroupLevel0FK ON
gl2.GLGroupLevel1FK =
[Dimension].GLGroupLevel1.[WHID]
INNER JOIN Fact.GLTransactionsMonthly trans ON acct.[WHID] =
trans.GLAccountFK
INNER JOIN [Dimension].GLProgram pgm ON trans.[GLProgramFK] =
pgm.[WHID] AND trans.[GLProgramFK] =
pgm.[WHID] AND trans.[GLProgramFK] = pgm.[WHID]
INNER JOIN [Dimension].[Date] d ON trans.EffectiveMonth = d.[Date]
AND trans.EffectiveMonth = d.[Date]
AND trans.EffectiveMonth = d.[Date]
INNER JOIN [Dimension].GLRegion ON pgm.GLRegionFK =
[Dimension].GLRegion.[WHID] AND pgm.GLRegionFK =
[Dimension].GLRegion.[WHID]
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
-- *** This, below -
WHERE (
(NOT (acct.[GLAccountCode] IN (N'9040',N'9047',N'4130')))
-- *** is the condition I want to execute, if my Parameter = False, and not
-- *** to execute, if my Parameter = True
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 (NOT (acct.[GLAccountCode] IN (N'9040',N'9047')))
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 (NOT (acct.[GLAccountCode] IN (N'4130')))
AND (acct.[GLAccountCode] BETWEEN '4000' AND '4999')
GROUP BY
f.[GLProgramFK]
HAVING
(SUM(f.[Amount]) <> 0))
)
OR
(pgm.[GLProgramCode] IN ('549'))
)
)
答案 0 :(得分:2)
赞:
(
(
@Depreciation = 0 /* FALSE AND EXISTING CONDITION */
and NOT (acct.[GLAccountCode] IN (N'9040',N'9047',N'4130'))
)
or
(
@Depreciation = 1 /* OR TRUE AND NOT EXISTING CONDITION */
and (acct.[GLAccountCode] IN (N'9040',N'9047',N'4130'))
)
)