我可以加入游标结果和子查询吗?

时间:2016-02-09 08:22:29

标签: sql sql-server sql-server-2012

DECLARE @year VARCHAR(20)
DECLARE @value NUMERIC

DECLARE db_cursor CURSOR
FOR

SELECT year
    ,sum(cast([value amount_CIS] AS NUMERIC))
FROM XYZ
GROUP BY year
ORDER BY 1

OPEN db_cursor

FETCH NEXT
FROM db_cursor
INTO @year
    ,@value

WHILE @@FETCH_STATUS = 0
BEGIN

 SELECT Result.Year
    ,Result.Value
    ,Result.Avgun
    ,Result.Uniqueshop
    ,Y.Growth
FROM (
    SELECT ResVal.yr AS Year
        ,ResVal.Total_Sales_Value AS Value
        ,ResSku.Avg_Skus AS Avgun
        ,ResVal.Unique_Outlet_Count AS Uniqueshop
    FROM (
        SELECT TOP 99.99999 PERCENT a.Year AS yr
            ,SUM(cast(A.[Value Amount_CIS] AS NUMERIC)) AS Total_Sales_Value
            ,COUNT(DISTINCT B.RET_CODE) AS Unique_Outlet_Count
        FROM XYZtable A
        INNER JOIN tblABC B ON A.[Ret Code] = B.RET_CODE
        GROUP BY a.Year
        ORDER BY a.Year
        ) ResVal
    INNER JOIN (
        SELECT TOP 99.99999 PERCENT a.YR
            ,sum(NUMBER_OF_SKUS) / sum(RetailerCode) AS Avg_Skus
        FROM (
            SELECT yr
                ,sum(NUMBER_OF_SKUS) NUMBER_OF_SKUS
            FROM (
                SELECT a.RetailerCode AS RC
                    ,year(a.DATE) AS YR
                    ,count(DISTINCT PRODUCT) AS NUMBER_OF_SKUS
                FROM FactSKU a
                INNER JOIN DIMSkuMaster b ON a.SKUCODE = b.SKUCODE
                --where year(a.date) = 2011 --and month(a.date) = 01 
                GROUP BY year(a.DATE)
                    ,a.RetailerCode
                ) t
            GROUP BY yr
            ) a
        INNER JOIN (
            SELECT year(DATE) yr
                ,count(DISTINCT RetailerCode) RetailerCode
            FROM FactSKU
            GROUP BY year(DATE)
            ) b ON a.YR = b.yr
        GROUP BY a.yr
        ORDER BY a.YR
        ) ResSku
    ) Result --Getting Error about incorrect syntax in the below line 
INNER JOIN (
    SELECT TOP 99.99999 PERCENT year
        ,(
            (
                @value - (
                    SELECT sum(cast([value amount_CIS] AS NUMERIC)) AS Value
                    FROM XYZ
                    WHERE year = @year - 1
                    )
                ) / (
                SELECT sum(cast([value amount_CIS] AS NUMERIC)) AS Value
                FROM XYZ
                WHERE year = @year - 1
                )
            ) * 100 AS Growth
    FROM XYZ
    WHERE year = @year
    GROUP BY year
    ) Y ----- Getting error  incorrect syntax
    ON Result.Year = Y.year

 --select @year,@value

 FETCH NEXT
FROM db_cursor
INTO @year
    ,@value END

CLOSE db_cursor

DEALLOCATE db_cursor

我正在尝试使用join将Cusrsor Results的复杂子查询的结果加入。 我在使用Y a a a alias和Result作为别名时遇到错误,因为它的语法不正确。我不能用这些吗?或者我怎么办? 并且它也在Y别名中抛出订单错误。

1 个答案:

答案 0 :(得分:1)

如果您尝试在游标块内的查询中使用游标中的变量,则没有问题。查询解析器告诉你真相,只有语法问题。你错过了JO作为JOIN的必要部分。 Chnage这个:

group by a.yr order by a.YR )ResSku) Result

进入这个

group by a.yr 
order by a.YR) ResSku ON 1=1 --put here some meaningful condition 
) Result 

当你试图写这样的查询时,我再给你一个建议。使其更具可读性,您可能会在第一时间看到问题。