0

我有以下脚本由于标题错误而停止工作。有人可以提供一些帮助吗?

SELECT DISTINCT TOP 100 PERCENT
         Locs.lCustomerGroupPK, Locs.lCustomerID, Locs.Customer_Group_Name, Locs.Customer_Name, Locs.Location_Name, TY.ThisYearsSales,
        (SELECT ThisYearsSales
           FROM dbo.Vw_Level_3_Sales_This_Year
          WHERE (lLocationID = Locs.lLocationID2 OR lLocationID = Locs.llocationid)
            AND (Current_Read_Date = TY.Current_Read_Date - 364) AND (ThisYearsSales <= 400)
         ) AS LastYearsSales, 
         TY.Current_Read_Date - 1 AS Current_Read_Date INTO #tmplocationlflsales
  FROM dbo.Vw_Level_3_Sales_This_Year AS TY 
        INNER JOIN dbo.vw_locations_Like_For_Like_Previous AS Locs 
                ON TY.lLocationID = Locs.llocationid OR TY.lLocationID = Locs.lLocationID2
 WHERE (TY.ThisYearsSales <= 400)
    AND (TY.Current_Read_Date = @RecordDate)
    AND TY.ThisYearsSales IS NOT NULL
    AND (SELECT ThisYearsSales
           FROM dbo.Vw_Level_3_Sales_This_Year
          WHERE (lLocationID = Locs.lLocationID2 OR lLocationID = Locs.llocationid) 
            AND (Current_Read_Date = TY.Current_Read_Date - 364) 
            AND (ThisYearsSales <= 400)
         ) IS NOT NULL 
 ORDER BY Locs.Customer_Group_Name, Locs.Customer_Name, Locs.Location_Name, Current_Read_Date
4

2 回答 2

0

问题似乎是 SELECT 中的子查询:

        SELECT ThisYearsSales
        FROM dbo.Vw_Level_3_Sales_This_Year
        WHERE (lLocationID = Locs.lLocationID2 OR 
                lLocationID = Locs.llocationid) 
            AND (Current_Read_Date = TY.Current_Read_Date - 364) 
            AND (ThisYearsSales <= 400)

这返回了太多行。也许您想将其更改为:

        SELECT sum(ThisYearsSales)
        FROM dbo.Vw_Level_3_Sales_This_Year
        WHERE (lLocationID = Locs.lLocationID2 OR 
                lLocationID = Locs.llocationid) 
            AND (Current_Read_Date = TY.Current_Read_Date - 364) 
            AND (ThisYearsSales <= 400)

由于您两次使用相同的子查询,因此您应该将它放在 FROM 子句中。

于 2012-07-25T13:07:31.943 回答
0

据我所知,您的脚本中有这个子查询

SELECT ThisYearsSales
FROM dbo.Vw_Level_3_Sales_This_Year
WHERE (lLocationID = Locs.lLocationID2 OR lLocationID = Locs.llocationid) 
 AND (Current_Read_Date = TY.Current_Read_Date - 364) 
 AND (ThisYearsSales <= 400)

如果这返回一个值(一条记录中的一列),那么它将包含在您的结果中,但是,如果这返回多于一行,您将收到您遇到的错误。

查看 Vw_Level_3_Sales_This_Year 视图中的数据以查看每个 lLocationID / Current_Read_Date 是否存在重复项,因为这些是您过滤的字段。类似于以下内容:

SELECT count(*), lLocationID, Current_Read_Date 
FROM dbo.Vw_Level_3_Sales_This_Year
GROUP BY lLocationID, Current_Read_Date 
HAVING count(*)>1

此查询返回的任何行都应指出问题所在。

于 2012-07-25T13:09:59.533 回答