Question

I have this SELECT statement:

SELECT SUM(dbo.DLData.Quantity)
    FROM dbo.MasterDates
    JOIN dbo.DLData ON MasterDates.[ID] = dbo.DLData.DownloadID
    WHERE dbo.MasterDates.[Date] BETWEEN @From AND @To
    AND dbo.MasterDates.SiteID = @X

If you plug in the @From, @To and @X it works. This is to be the right hand side of a comparison. However in the comparison the @X needs to come from the left hand side of the as the SELECT on the outside is performing test on every SiteID in the set. Without this clause the main clause works. They need to go together. Here is the full select statement with the above select left in. Eventually the two having clauses will determine the key products for a site.

--INSERT INTO SiteKeyProducts
    SELECT dbo.MasterDates.SiteID as SiteID , dbo.DLData.Product as ProductID
        FROM dbo.MasterDates
        JOIN dbo.DLData ON dbo.MasterDates.[ID] = dbo.DLData.DownloadID
        WHERE dbo.MasterDates.[Date] BETWEEN @From AND @To
        GROUP BY dbo.MasterDates.SiteID , dbo.DLData.Product
        HAVING
            SUM(dbo.DLData.Quantity) > 960 OR
            SUM(dbo.DLData.Quantity) > (SELECT SUM(dbo.DLData.Quantity)
                            FROM dbo.MasterDates
                            JOIN dbo.DLData ON MasterDates.[ID] = dbo.DLData.DownloadID
                            WHERE dbo.MasterDates.[Date] BETWEEN @From AND @To
                            AND dbo.MasterDates.SiteID = @X)
        ORDER BY dbo.MasterDates.SiteID 

How do you use the outer clause on an inner statement? Hell, I don't even know if I am using the correct terminology to describe my problem.

Arrgghh - Set based logic does my head in!!

Was it helpful?

Solution 2

Matt Whitfield on the ask.sqlservercentral StackExchange site came up with the following answer to this problem:

SELECT outerMaster.SiteID as SiteID , dbo.DLData.Product as ProductID
    FROM dbo.MasterDates outerMaster
    JOIN dbo.DLData ON outerMaster.[ID] = dbo.DLData.DownloadID
    WHERE outerMaster.[Date] BETWEEN @From AND @To
    GROUP BY outerMaster.SiteID , dbo.DLData.Product
    HAVING
            SUM(dbo.DLData.Quantity) > 960 OR
            SUM(dbo.DLData.Quantity) > (SELECT SUM(dbo.DLData.Quantity)
                                          FROM dbo.MasterDates innerMaster
                                          JOIN dbo.DLData ON innerMaster.[ID] = dbo.DLData.DownloadID
                                            WHERE innerMaster.[Date] BETWEEN @From AND @To
                                              AND innerMaster.SiteID = outerMaster.SiteID)
    ORDER BY outerMaster.SiteID

And RickD came up with this answer on the ask.sqlteam.com site:

--INSERT INTO SiteKeyProducts    
SELECT 
        MD.SiteID as SiteID , 
        DLD.Product as ProductID        
FROM dbo.MasterDates MD      
JOIN dbo.DLData DLD
ON MD.[ID] = DLD.DownloadID
LEFT JOIN (SELECT SiteID, 
                                SUM(dbo.DLData.Quantity) SumMDQuantity
                                FROM dbo.MasterDates
                                JOIN dbo.DLData ON MasterDates.[ID] = dbo.DLData.DownloadID
                                WHERE dbo.MasterDates.[Date] BETWEEN @From AND @To
                                GROUP BY SiteID) as sumMD
ON sumMD.SiteID = MD.SiteID
WHERE MD.[Date] BETWEEN @From AND @To        
GROUP BY MD.SiteID , DLD.Product        
HAVING                
SUM(DLD.Quantity) > 960 OR                
SUM(DLD.Quantity) > sumMD.SumMDQuantity
ORDER BY MD.SiteID

Both work great, though Matts bests Ricks effort despite my initial view that a Join would be faster. Matts takes ~2m 10s while Ricks takes ~2m 40s. This is for the largest live database as both takes less than 10s on the test db.

OTHER TIPS

I think you want to join this subquery, not use it in a having statement

SELECT SUM(dbo.DLData.Quantity)
    FROM dbo.MasterDates
    JOIN dbo.DLData ON MasterDates.[ID] = dbo.DLData.DownloadID
    WHERE dbo.MasterDates.[Date] BETWEEN @From AND @To
    AND dbo.MasterDates.SiteID = @X
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top