在右侧SQL比较中使用左手值

时间:2009-10-19 15:52:23

标签: select sql-server-2000

我有SELECT声明:

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

如果您插入@From@To@X,则可以正常运行。这是比较的右手边。但是,在比较中,@X需要来自左侧,因为外部的SELECT正在对集合中的每个SiteID执行测试。如果没有此子句,主条款就可以工作他们需要一起去。这是完整的选择语句,上面的选择左边。最后两个有条款将确定一个站点的关键产品。

--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 

你如何在内部陈述中使用外部条款?天啊,我甚至不知道我是否使用正确的术语来描述我的问题。

Arrgghh - 基于集合的逻辑让我很满意!

2 个答案:

答案 0 :(得分:1)

我认为您想要加入此子查询,而不是在有声明

中使用它
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

答案 1 :(得分:0)

Matt Whitfield StackExchange网站上的

ask.sqlservercentral提出了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

RickDask.sqlteam.com网站上提出了这个答案:

--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

虽然我最初认为加入会更快,但Matts仍然很努力,但两者都很出色。 Matts需要大约2m 10s而Ricks需要大约2m 40s。这是针对最大的实时数据库,因为测试数据库上的数据都不到10秒。