我有这样的查询:
SELECT AVG( [ DC ]. [ ContractedAmount ]) AS [AverageContractedAmount]
, MAX([DC].[ContractedAmount]) AS [MaxContractedAmount]
, MIN([DC].[ContractedAmount]) AS [MinContractedAmount]
FROM [DesignCustomer] AS [DC]
INNER JOIN [Design] AS [D] ON [DC].[DesignKey] = [D].[DesignKey]
INNER JOIN [Task] AS [T] ON [D].[DesignKey] = [t].[DesignKey]
INNER JOIN [ProjectDesign] AS [PD] ON [D].[DesignKey] = [PD].[DesignKey]
INNER JOIN [Project] AS [P] ON [PD].[ProjectKey] = [P].[ProjectKey]
INNER JOIN [Address] AS [A] ON [A].[AddressGuid] = [P].[ProjectGuid]
WHERE [DC].[ContractedAmount] != 0.00
AND [D].StatusKey = 66
AND (@MinWoodStories IS NULL OR [P].[WoodStories] BETWEEN @MinWoodStories AND @MaxWoodStories)
AND (@MinNumberOfUnits IS NULL OR [P].[NumberOfUnits] BETWEEN @MinNumberOfUnits AND @MaxNumberOfUnits)
AND (@MinSqrFootage IS NULL OR [P].[SquareFootage] BETWEEN @MinSqrFootage AND @MaxSqrFootage)
AND (@MinNumberOfSegs IS NULL OR [P].[Bldg_Segs] BETWEEN @MinNumberOfSegs AND @MaxNumberOfSegs)
AND ([DC].[CustomerKey] = @CustomerKey
OR [A].[RegionKey] = @RegionKey
OR [A].[StateKey] = @StateKey)
如您在我的最后一个AND
子句中所见,我有“ RegionKey = @RegionKey”
AND ([DC].[CustomerKey] = @CustomerKey
OR [A].[RegionKey] = @RegionKey
OR [A].[StateKey] = @StateKey)
在某些情况下,我会收到@RegionKey = 0,这在我的数据库中不作为寄存器存在。案例0在我的代码中表示“全部”,因此我想知道如何在@RegioKey参数中收到0的情况下修改此查询,只需删除regionKey过滤器即可。我该如何实现?
答案 0 :(得分:2)
AND ([DC].[CustomerKey] = @CustomerKey
OR [A].[RegionKey] = @RegionKey OR @RegionKey = 0
OR [A].[StateKey] = @StateKey)