0

在以下查询中,我尝试使用此查询获取所有 isrc 的持续时间小于 300000 毫秒的 upcs,其中轨道数为 11,并且拥有区域不在 31,201,41,125 中)

select  r.UPC ,r.Id, res.ISRC , res.Duration ,COUNT( res.ISRC) from Release r 
inner join ReleaseResource rr on rr.ReleaseId=r.Id
inner join Resource res on res.Id=rr.ResourceId
inner join ReleaseTerritory rt on rt.ReleaseId=r.id
where   not r.OwningTerritoryId in (31,201,41,125) and res.Duration<5*60000 and  r.TrackCount=11 and rt.IsDeleted=0
group by r.UPC ,r.Id, res.ISRC , res.Duration
having COUNT( distinct rt.TerritoryId)=10  
order by r.upc

我得到的结果很好,除了 isrc 查询仅显示小于 300000 毫秒的 isrc 但是当我查看 upc 内部时,我发现还有其他持续时间超过 300000 毫秒的 isrc。你有什么我应该修改的,以便只有所有 isrc 小于 300000 毫秒的 upcs。谢谢

4

1 回答 1

1

Its because you are filtering out the 'duration less than 300000 ms' before the GROUPING .. Remove the filter from WHERE and try this in HAVING

select  r.UPC ,r.Id, res.ISRC , res.Duration ,COUNT( res.ISRC) from Release r 
inner join ReleaseResource rr on rr.ReleaseId=r.Id
inner join Resource res on res.Id=rr.ResourceId
inner join ReleaseTerritory rt on rt.ReleaseId=r.id
where   not r.OwningTerritoryId in (31,201,41,125) and  r.TrackCount=11 and rt.IsDeleted=0
group by r.UPC ,r.Id, res.ISRC , res.Duration
having COUNT( distinct rt.TerritoryId)=10  AND MAX(res.Duration)<5*60000
order by r.upc
于 2013-06-14T14:48:16.147 回答