消息512,级别16,状态1,子查询返回的值超过1

时间:2018-11-28 09:36:06

标签: sql sql-server

这是查询错误所指向的,不知道出了什么问题?

SELECT ISNULL(SUM(a.classes),0) 
from (SELECT DISTINCT a.for_date, COUNT(DISTINCT classID) as classes 
      FROM School_Classes sc 
      INNER JOIN app_attendance a on a.schoolclass_id = sc.Id 
      WHERE schoolID =@SchoolID AND a.for_date BETWEEN 
      DATEADD(DAY,-30,getdate()) AND getdate() GROUP BY a.for_date
      ) a

1 个答案:

答案 0 :(得分:0)

存储过程如下:

USE [smsdb]
GO
/****** Object:  StoredProcedure [dbo].[DailyAlertGSP]    Script Date: 28/11/2018 2:47:47 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:      <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER PROCEDURE [dbo].[DailyAlertGSP] 
    -- Add the parameters for the stored procedure here
    @RegionIn varchar(150),
    @isArea bit
    --<@Param2, sysname, @p2> <Datatype_For_Param2, , int> = <Default_Value_For_Param2, , 0>
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;
    DECLARE @sumAgeing int,@countAgeing int,@sumTotalClasses int,@sumClassesAttendance int,@sumStudentStrength int
    -- Insert statements for procedure here
    DECLARE @SchoolID INT ,@principalID VARCHAR(50),@ageing INT,@totalClasses INT,@classesAttendance INT,@percentage FLOAT,@ssr INT,@noDays INT
    DECLARE @region varchar(150),@area VARCHAR(150),@schoolName VARCHAR(150),@principalName VARCHAR(150)
    DECLARE @StartDate date,@EndDate date
DECLARE @FinalData TABLE (Region VARCHAR(150), Area VARCHAR(150), [School ID] INT, [School Name] VARCHAR(150), [Principal Code] INT, [Principal Name] VARCHAR(150),
Ageing INT, [Total Classes] INT, [Classes Attendance] INT, [Precentage (%)] FLOAT, [Student Strength] INT) 

iF @isArea = 1 
    DECLARE AllSchools CURSOR FOR 
    SELECT s.schoolID as school FROM dbo.Region r INNER JOIN dbo.Area a ON r.regionID = a.regionID 
    INNER JOIN dbo.Location l ON a.areaID = l.areaID INNER JOIN Campus c on c.locationid = l.locationid INNER JOIN School s on s.campusID = c.id
    --INNER JOIN SchoolEMIS se ON s.schoolID=se.schoolID 
    WHERE a.Name like @RegionIn AND s.isDeleted = 0 order by r.Name;
ELSE 
    DECLARE AllSchools CURSOR FOR 
    SELECT s.schoolID as school FROM dbo.Region r INNER JOIN dbo.Area a ON r.regionID = a.regionID 
    INNER JOIN dbo.Location l ON a.areaID = l.areaID INNER JOIN Campus c on c.locationid = l.locationid INNER JOIN School s on s.campusID = c.id 
    --INNER JOIN SchoolEMIS se ON s.schoolID=se.schoolID
    WHERE r.Name like @RegionIn AND s.isDeleted = 0 order by r.Name;

OPEN AllSchools
FETCH NEXT FROM AllSchools INTO @SchoolID


WHILE @@FETCH_STATUS = 0
BEGIN 
    SET @StartDate = GETDATE(); SET @EndDate = DATEADD(DAY,-30,getdate())
    SET @schoolName = (SELECT Name FROM School WHERE schoolID=@SchoolID)
    IF @isArea = 1
    SET @region = (Select r.Name FROM dbo.Region r INNER JOIN dbo.Area a ON r.regionID = a.regionID 
                   INNER JOIN dbo.Location l ON a.areaID = l.areaID INNER JOIN Campus c on c.locationid = l.locationid INNER JOIN School s on s.campusID = c.id 
                   WHERE a.Name like @RegionIn AND s.schoolID = @SchoolID )
    ELSE
    SET @region = (Select r.Name FROM dbo.Region r INNER JOIN dbo.Area a ON r.regionID = a.regionID 
                   INNER JOIN dbo.Location l ON a.areaID = l.areaID INNER JOIN Campus c on c.locationid = l.locationid INNER JOIN School s on s.campusID = c.id 
                   WHERE r.Name like @RegionIn AND s.schoolID = @SchoolID )
    IF @isArea = 1
    SET @area = (Select a.Name FROM dbo.Region r INNER JOIN dbo.Area a ON r.regionID = a.regionID 
                 INNER JOIN dbo.Location l ON a.areaID = l.areaID INNER JOIN Campus c on c.locationid = l.locationid INNER JOIN School s on s.campusID = c.id 
                 WHERE a.Name like @RegionIn AND s.schoolID = @SchoolID )
    ELSE
    SET @area = (Select a.Name FROM dbo.Region r INNER JOIN dbo.Area a ON r.regionID = a.regionID 
                 INNER JOIN dbo.Location l ON a.areaID = l.areaID INNER JOIN Campus c on c.locationid = l.locationid INNER JOIN School s on s.campusID = c.id 
                 WHERE r.Name like @RegionIn AND s.schoolID = @SchoolID )
    SET @principalID = (SELECT [Employee Code] FROM [hcmdbserv].[HCM_LIVE].[dbo].[vw_BI_Principal_Info] WHERE [Job Status] = 'Active' AND [End Date_H] is NULL 
                        AND [School ID_K] = @SchoolID)
    SET @principalName = (SELECT ISNULL(First_Name,'') +' '+ISNULL(Name,'') FROM [hcmdbserv].[HCM_LIVE].[dbo].[vw_BI_Principal_Info] WHERE [Job Status] = 'Active' AND [End Date_H] is NULL 
                          AND [School ID_K] = @SchoolID)
    SET @ageing = (SELECT (SELECT DateDiff(DAY,(SELECT MAX([TimeStamp]) as LastDate),getdate())) FROM app_AuditLog WHERE InFunction ='StudentDataController.Get' AND OnTask = 'School'  AND ObjectData = @SchoolID) 
    SET @ssr = (select top 1 totalStrength from Academics_Master where schoolId = @SchoolID order by id desc)
    SET @noDays = (SELECT dbo.ufn_GetWeekDays(@EndDate,@StartDate))
    SET @totalClasses = ((SELECT COUNT(*) FROM School_Classes WHERE schoolID=@SchoolID)*@noDays)
    SET @classesAttendance = (SELECT ISNULL(SUM(a.classes),0) from (SELECT DISTINCT a.for_date, COUNT(DISTINCT classID) as classes FROM School_Classes sc 
                                INNER JOIN app_attendance a on a.schoolclass_id = sc.Id WHERE schoolID =@SchoolID AND a.for_date BETWEEN DATEADD(DAY,-30,getdate()) 
                                AND getdate() GROUP BY a.for_date) a)
    if (@classesAttendance = 0) BEGIN SET @percentage = 0 END
    ELSE BEGIN SET @percentage = (@classesAttendance *100.0 /@totalClasses) END
    IF (@principalID is NULL) BEGIN SET @principalID = '' END
    IF (@principalName is NULL) BEGIN SET @principalName = '' END
    IF (@ageing IS NULL) BEGIN SET @ageing = 0 END
    IF (@totalClasses IS NULL) BEGIN SET @totalClasses = 0 END
    IF (@classesAttendance IS NULL) BEGIN SET @classesAttendance = 0 END
    IF (@percentage IS NULL) BEGIN SET @percentage = 0 END
    IF (@ssr IS NULL) BEGIN SET @ssr = 0 END



    INSERT INTO @FinalData (Region,Area,[School ID],[School Name],[Principal Code],[Principal Name],Ageing,[Student Strength],[Classes Attendance],[Total Classes],[Precentage (%)])
    VALUES (@region,@area,@SchoolID,@schoolName,@principalID,@principalName,@ageing,@ssr,@classesAttendance,@totalClasses,@percentage)

    FETCH NEXT FROM AllSchools INTO @SchoolID
END 

CLOSE AllSchools
DEALLOCATE AllSchools


SELECT * FROM @FinalData fd order by fd.[Precentage (%)] asc, fd.Ageing desc,fd.Area asc,fd.Region 
SET @sumTotalClasses= (SELECT SUM([Total Classes]) FROM @FinalData)
SET @sumAgeing= (SELECT SUM(Ageing) FROM @FinalData)
SET @countAgeing = (SELECT COUNT(Ageing) FROM @FinalData)
SET @sumClassesAttendance= (SELECT SUM([Classes Attendance]) FROM @FinalData)
SET @sumStudentStrength= (SELECT SUM([Student Strength]) FROM @FinalData)
--SET @averagePercentage= (SELECT CAST( (SUM([Classes Attendance])/SUM([Total Classes]) * 100) as FLOAT) FROM @FinalData)
SELECT @sumAgeing as [Sum Ageing],@countAgeing as [Count Ageing],@sumClassesAttendance as [Sum Classes],@sumStudentStrength as [Sum Student],
@sumTotalClasses as [Sum Total]
END