0

在此查询中,我试图获取 31、41、125、201 区域的价格相同的值(主要是 r.dpq) 其中价格应为922,而对于所有其他区域,价格应为 216

该查询确实在 31 , 41 , 125 , 201 地区获取了价格为 216 的产品,但在其他地区没有,我猜错误在这里,但我不知道如何纠正它

rte.TerritoryId not in (31,41,201,125) and rte.PriceCodeId=216 and rte.PartnerTerritoryId is null

这是完整的查询

select r.dpq, r.OwningTerritoryId , r.id from Release r
inner join ReleaseTerritory rt on rt.ReleaseId=r.Id
inner join ReleaseTerritoryPrice rtp on rtp.ReleaseId=r.Id
where exists (
select * from ReleaseTerritoryPrice rtp31, ReleaseTerritoryPrice rtp201,ReleaseTerritoryPrice rtp41,ReleaseTerritoryPrice rtp125,
ReleaseTerritory rt69 , ReleaseTerritoryPrice rte
where 
rtp31.ReleaseId=r.Id and rtp201.ReleaseId=r.Id and rtp41.ReleaseId=r.Id and rtp125.ReleaseId=r.Id and rt69.ReleaseId=r.Id and 
rte.ReleaseId=r.Id and 
rtp31.TerritoryId=31 and rtp201.TerritoryId=201 and rtp41.TerritoryId=41 and rtp125.TerritoryId=125 and 
rtp31.PriceCodeId=rtp201.PriceCodeId and rtp201.PriceCodeId=rtp41.PriceCodeId and rtp41.PriceCodeId=rtp125.PriceCodeId and 
rt69.TerritoryId=69 and 
rtp31.PriceCodeId=922 and 
rt69.IsLocked=0 and 
rte.TerritoryId not in (31,41,201,125) and rte.PriceCodeId=216 and rte.PartnerTerritoryId is null
) and r.OwningTerritoryId in (201,200)
group by r.dpq , r.OwningTerritoryId , r.Id
order by r.OwningTerritoryId

谢谢你的帮助

4

2 回答 2

1

将 where 子句的最后部分修改为:

AND (
       (rte.TerritoryId IN (31,41,201,125) AND rte.PriceCodeId = 922)     
        OR
       (rte.TerritoryId NOT IN (31,41,201,125) AND rte.PriceCodeId = 216)
    )

请注意语句开头和结尾处的附加括号——这些括号对于保持 OR 仅本地化到该语句而不是使AND完整查询中的其余语句无效很重要。

您还必须从 WHERE 子句中删除此语句,因为它将结果仅限于这 4 个地区

rtp31.TerritoryId=31 and rtp201.TerritoryId=201 
and rtp41.TerritoryId=41 and rtp125.TerritoryId=125 
于 2013-06-06T11:19:00.547 回答
1

我认为您提到的过滤器不是您的要求。它可能像

(rte.TerritoryId not in (31,41,201,125) and rte.PriceCodeId=216) or
(rte.TerritoryId not (31,41,201,125) and rte.PriceCodeId=922)
于 2013-06-06T11:19:07.140 回答