我希望根据Sql Server 2008中的用户定义参数将结果连接到两个表和组。我尝试了下面的查询,但它无法正常工作。如果有人帮助我看到我的错误并为我纠正错误,我感激不尽。问候。
Select TotalVolume= SUM(volume),
PrimGroup = r.PrimaryGroup, SnGroup = r.SecondaryGroup
from Requests r
inner join #Calculations c on
case @PrimaryId is not null then c.PrimaryGroup = r.PrimaryGroup end
and case @SecondaryId is not null then c.SecondaryGroup = r.SecondaryGroup end
and c.SrgId = r.SrgId
group by
case @PrimaryId is not null then r.PrimaryGroup end,
case @SecondaryId is not null then r.SecondaryGroup end
答案 0 :(得分:1)
试试这个:
Select TotalVolume= SUM(volume),
PrimGroup = r.PrimaryGroup, SnGroup = r.SecondaryGroup
from Requests r
inner join #Calculations c on
((@PrimaryId is null) or
(@PrimaryId is not null) and c.PrimaryGroup = r.PrimaryGroup)
and ((@SecondaryId is null) or
(@SecondaryId is not null) and c.SecondaryGroup = r.SecondaryGroup)
and c.SrgId = r.SrgId
group by
case when @PrimaryId is not null then r.PrimaryGroup end,
case when @SecondaryId is not null then r.SecondaryGroup end