D
dlawner
Using a three table join, I am trying to get an aggregate sum of one
field from one table and the count for a field in another table in the
same recordset. The problem is that if multple records are returned
from the join, the aggregates are multiplied by the number of records
returned. The query that I was trying to use is the following:
SELECT s.showID, s.Headline Headline, s.Status, s.LastEditedDate
LastEditedDate, s.AffiliateNo, s.LastEditedBy LastEditedBy,
u.FirstName, u.LastName, count(c.clipid) NumClips, sum(p.SizeMB) SizeMB
FROM VISShow s WITH (READPAST), VISVideoProxy p with
(index(inx_VISVideoProxy_showid), READPAST), VISClip c
(index(inx_visclip_affiliateno),READPAST), AffiliateUser u (READPAST)
WHERE c.showid =* s.showid
AND s.ShowID = p.ShowID
AND c.status in('L', 'D')
AND s.LastEditedBy = u.UserNo
AND c.AffiliateNo = 5
And s.AffiliateNo = 5
AND s.Status = 'L'
GROUP BY s.showID, s.Headline, s.Status, s.LastEditedDate,
s.LastEditedBy, s.AffiliateNo, u.FirstName, u.LastName
I tried the following which works if I didn't need an outer join for
the num clips in the second join
SELECT x.showID, x.SizeMB, y.NumClips
FROM
(
SELECT s.showID, sum(p.SizeMB) as SizeMB
FROM VISShow s WITH (READPAST), VISVideoProxy p with
(index(inx_VISVideoProxy_showid), READPAST), AffiliateUser u
(READPAST)
WHERE s.ShowID = p.ShowID
AND s.LastEditedBy = u.UserNo
And s.AffiliateNo = 5
AND s.Status = 'L'
GROUP BY s.showID
) as x
JOIN
(
SELECT s.showID, count(c.clipid) as NumClips
FROM VISShow s WITH (READPAST), VISClip c
(index(inx_visclip_affiliateno),READPAST)
WHERE c.showid =* s.showid
AND c.status in('L', 'D')
AND c.AffiliateNo = 5
And s.AffiliateNo = 5
AND s.Status = 'L'
GROUP BY s.showID
) as y on (y.showID = x.showID)
Any other ideas short of using a temp table would be appreciated.
field from one table and the count for a field in another table in the
same recordset. The problem is that if multple records are returned
from the join, the aggregates are multiplied by the number of records
returned. The query that I was trying to use is the following:
SELECT s.showID, s.Headline Headline, s.Status, s.LastEditedDate
LastEditedDate, s.AffiliateNo, s.LastEditedBy LastEditedBy,
u.FirstName, u.LastName, count(c.clipid) NumClips, sum(p.SizeMB) SizeMB
FROM VISShow s WITH (READPAST), VISVideoProxy p with
(index(inx_VISVideoProxy_showid), READPAST), VISClip c
(index(inx_visclip_affiliateno),READPAST), AffiliateUser u (READPAST)
WHERE c.showid =* s.showid
AND s.ShowID = p.ShowID
AND c.status in('L', 'D')
AND s.LastEditedBy = u.UserNo
AND c.AffiliateNo = 5
And s.AffiliateNo = 5
AND s.Status = 'L'
GROUP BY s.showID, s.Headline, s.Status, s.LastEditedDate,
s.LastEditedBy, s.AffiliateNo, u.FirstName, u.LastName
I tried the following which works if I didn't need an outer join for
the num clips in the second join
SELECT x.showID, x.SizeMB, y.NumClips
FROM
(
SELECT s.showID, sum(p.SizeMB) as SizeMB
FROM VISShow s WITH (READPAST), VISVideoProxy p with
(index(inx_VISVideoProxy_showid), READPAST), AffiliateUser u
(READPAST)
WHERE s.ShowID = p.ShowID
AND s.LastEditedBy = u.UserNo
And s.AffiliateNo = 5
AND s.Status = 'L'
GROUP BY s.showID
) as x
JOIN
(
SELECT s.showID, count(c.clipid) as NumClips
FROM VISShow s WITH (READPAST), VISClip c
(index(inx_visclip_affiliateno),READPAST)
WHERE c.showid =* s.showid
AND c.status in('L', 'D')
AND c.AffiliateNo = 5
And s.AffiliateNo = 5
AND s.Status = 'L'
GROUP BY s.showID
) as y on (y.showID = x.showID)
Any other ideas short of using a temp table would be appreciated.