SQL Server子查询问题和解释

时间:2018-11-28 13:34:07

标签: sql-server

我写了以下报告(我知道它还有其他问题需要解决),但是我遇到了一个错误。

请您解释一下该错误的含义。

  

SQL Server子查询返回了多个值。当子查询遵循=,!=,<,<=,>,> =

时,不允许这样做

我在报告中的错误也会抛出此错误。

--SELECT Database Name
USE FEN;

--DECLARE Variables
DECLARE @Yesterday INT;
DECLARE @YesterdayLastYear INT;
DECLARE @YTD1 DECIMAL(10,2)
DECLARE @LastWeek INT;
DECLARE @LastWeekLastYear INT;
DECLARE @YTD2 DECIMAL (10,2);

-- SET Variables value with queries
SET @Yesterday = (SELECT SUM (ri.Sales_Quantity) FROM  ReportTransactions AS RT     LEFT JOIN ReportItems AS ri ON rt.Report_Transaction_ID = ri.Report_Transaction_ID      LEFT JOIN MMGroups AS mmg ON ri.MMGroup2_ID = MMG.ID    LEFT JOIN Locations AS L ON L.Location_ID = RT.Store_ID WHERE   rt.Transaction_Date > DATEADD(DAY, -1, GETDATE()) AND   (NULL IS NULL OR rt.Store_ID = NULL)   AND  (rt.Training_Mode IS NULL OR rt.Training_Mode = 0)   AND    ri.Item_Voided = 0   AND    ri.Void_Type = 0   AND  rt.Transaction_Type IN (0, 1, 2, 3, 4, 5, 6)   AND  ri.Item_Type IN (1, 2, 3, 4, 6, 7, 54, 55, 81, 84, 11, 12, 13, 22, 25, 27, 82) GROUP BY     RT.Store_ID,    L.Name, CASE WHEN ri.MMGroup2_ID IS NULL OR ri.MMGroup2_ID = '' THEN '99999' ELSE ri.MMGroup2_ID END,   CASE WHEN ri.MMGroup3_ID IS NULL OR ri.MMGroup3_ID = '' THEN '99999' ELSE ri.MMGroup3_ID END  );
SET @YesterdayLastYear = (SELECT SUM (ri.Sales_Quantity) FROM  ReportTransactions AS RT     LEFT JOIN ReportItems AS ri ON rt.Report_Transaction_ID = ri.Report_Transaction_ID      LEFT JOIN MMGroups AS mmg ON ri.MMGroup2_ID = MMG.ID    LEFT JOIN Locations AS L ON L.Location_ID = RT.Store_ID WHERE   rt.Transaction_Date > CONVERT(DateTime, DATEDIFF(DAY, 0, DATEADD(year, -1, GETDATE()))) AND rt.Transaction_Date < DATEADD(hour, 23, DATEADD(d,0,DATEDIFF(d,0, DATEADD(year, -1, GETDATE())))) AND   (NULL IS NULL OR rt.Store_ID = NULL)   AND  (rt.Training_Mode IS NULL OR rt.Training_Mode = 0)   AND    ri.Item_Voided = 0   AND    ri.Void_Type = 0   AND  rt.Transaction_Type IN (0, 1, 2, 3, 4, 5, 6)   AND  ri.Item_Type IN (1, 2, 3, 4, 6, 7, 54, 55, 81, 84, 11, 12, 13, 22, 25, 27, 82) GROUP BY     RT.Store_ID,    L.Name, CASE WHEN ri.MMGroup2_ID IS NULL OR ri.MMGroup2_ID = '' THEN '99999' ELSE ri.MMGroup2_ID END,   CASE WHEN ri.MMGroup3_ID IS NULL OR ri.MMGroup3_ID = '' THEN '99999' ELSE ri.MMGroup3_ID END  );
SET @YTD1 = (SELECT  CAST(@Yesterday AS FLOAT)/CAST(@YesterdayLastYear AS FLOAT) * 100);
SET @LastWeek = (SELECT SUM(ri.Sales_Quantity) FROM  ReportTransactions AS RT   LEFT JOIN ReportItems AS ri ON rt.Report_Transaction_ID = ri.Report_Transaction_ID      LEFT JOIN MMGroups AS mmg ON ri.MMGroup2_ID = MMG.ID    LEFT JOIN Locations AS L ON L.Location_ID = RT.Store_ID WHERE   rt.Transaction_Date BETWEEN DATEADD(DAY, -7, GETDATE()) AND DATEADD(DAY, 1, GETDATE()) AND  (NULL IS NULL OR rt.Store_ID = NULL)   AND  (rt.Training_Mode IS NULL OR rt.Training_Mode = 0)   AND    ri.Item_Voided = 0   AND    ri.Void_Type = 0   AND  rt.Transaction_Type IN (0, 1, 2, 3, 4, 5, 6)   AND  ri.Item_Type IN (1, 2, 3, 4, 6, 7, 54, 55, 81, 84, 11, 12, 13, 22, 25, 27, 82) GROUP BY     RT.Store_ID,    L.Name, CASE WHEN ri.MMGroup2_ID IS NULL OR ri.MMGroup2_ID = '' THEN '99999' ELSE ri.MMGroup2_ID END,   CASE WHEN ri.MMGroup3_ID IS NULL OR ri.MMGroup3_ID = '' THEN '99999' ELSE ri.MMGroup3_ID END);
SET @LastWeekLastYear = 1000;
SET @YTD2 = (SELECT CAST(@LastWeek AS FLOAT)/CAST(@LastWeekLastYear AS FLOAT) * 100)

--SELECT report columns 
SELECT 
    RT.Store_ID,
    L.Name,
    (SELECT @Yesterday) AS 'Sales Day £',   
    (SELECT @YesterdayLastYear) AS 'VAR Vs LY £',
    (SELECT @YTD1) AS 'VAR Vs LY %',
    (SELECT @LastWeek) AS 'Sales Week to Date £',
    (SELECT @LastWeekLastYear) AS 'VAR Vs LY £k',
    (SELECT @YTD2)  AS 'VAR Vs LY %'
--From Required Tables and Joins    
FROM ReportTransactions AS RT 
    LEFT JOIN ReportItems AS ri ON rt.Report_Transaction_ID = ri.Report_Transaction_ID  
    LEFT JOIN MMGroups AS mmg ON ri.MMGroup2_ID = MMG.ID
    LEFT JOIN Locations AS L ON L.Location_ID = RT.Store_ID      
-- Group by Store Number and Name 
GROUP BY 
    RT.Store_ID,
    L.Name

0 个答案:

没有答案