Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi All,

I have the following long query and I cant seem to get it working the way I would like! The error I get is:

Msg 512, Level 16, State 1, Line 31
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.


Can anyone tell me what/how I need to change this to get it working please?

The output should look something like the following in SSRS. Then I can have an expression to calculate events and points etc...

Boarding House | + Events | + Points | - Events | - Points
House 1        | 10       | 20       | 10       | 10
House 2        | 5        | 10       | 5        | 5
etc...

I appreciate its quite complex, but any help/guidance would great!

Thanx in advance.



SQL
SELECT	PMP.txtBoardingHouse,

(SELECT	COUNT(RMRTC.intValue) AS exp1
FROM	TblPupilManagementPupils AS PMP 
	INNER JOIN TblRewardsManagerRewards AS RMR ON PMP.txtSchoolID = RMR.txtSchoolID 
	INNER JOIN TblRewardsManagerRewardTypes AS RMRT 
	INNER JOIN TblRewardsManagerRewardTypesCategories AS RMRTC 
                ON RMRT.TblRewardsManagerRewardTypesID = RMRTC.intType 
        	ON RMR.intCategory = RMRTC.TblRewardsManagerRewardTypesCategoriesID 
            AND RMR.intType = RMRT.TblRewardsManagerRewardTypesID
	WHERE	(RMRT.intPositive = 1) AND (PMP.txtSchoolID = RMR.txtSchoolID) 
            AND (RMR.txtDate BETWEEN '2013-01-01' AND '2014-01-01') 
            AND (RMRT.TblRewardsManagerRewardTypesID BETWEEN 19 AND 24) 
            AND (RMRTC.TblRewardsManagerRewardTypesCategoriesID BETWEEN 63 and 97)
	GROUP BY PMP.txtBoardingHouse) AS '+ Events',
 
(SELECT	SUM(RMRTC.intValue) AS exp2
FROM	TblPupilManagementPupils AS PMP 
        INNER JOIN TblRewardsManagerRewards AS RMR ON PMP.txtSchoolID = RMR.txtSchoolID 
	INNER JOIN TblRewardsManagerRewardTypes AS RMRT 
	INNER JOIN TblRewardsManagerRewardTypesCategories AS RMRTC 
                ON RMRT.TblRewardsManagerRewardTypesID = RMRTC.intType 
		ON RMR.intCategory = RMRTC.TblRewardsManagerRewardTypesCategoriesID 
            AND RMR.intType = RMRT.TblRewardsManagerRewardTypesID
	WHERE	(RMRT.intPositive = 1) AND (PMP.txtSchoolID = RMR.txtSchoolID) 
            AND (RMR.txtDate BETWEEN '2013-01-01' AND '2014-01-01') 
            AND (RMRT.TblRewardsManagerRewardTypesID BETWEEN 19 AND 24) 
            AND (RMRTC.TblRewardsManagerRewardTypesCategoriesID BETWEEN 63 and 97)
	GROUP BY PMP.txtBoardingHouse) AS '+ Points', 

(SELECT	COUNT(RMRTC.intValue) AS exp3
FROM	TblPupilManagementPupils AS PMP 
			INNER JOIN TblRewardsManagerRewards AS RMR ON PMP.txtSchoolID = RMR.txtSchoolID 
			INNER JOIN TblRewardsManagerRewardTypes AS RMRT 
			INNER JOIN TblRewardsManagerRewardTypesCategories AS RMRTC ON RMRT.TblRewardsManagerRewardTypesID = RMRTC.intType 
						ON RMR.intCategory = RMRTC.TblRewardsManagerRewardTypesCategoriesID AND RMR.intType = RMRT.TblRewardsManagerRewardTypesID
	WHERE	(RMRT.intPositive = - 1) AND (PMP.txtSchoolID = RMR.txtSchoolID) AND (RMR.txtDate BETWEEN '2013-01-01' AND '2014-01-01') 
			AND (RMRT.TblRewardsManagerRewardTypesID BETWEEN 19 AND 24) AND (RMRTC.TblRewardsManagerRewardTypesCategoriesID BETWEEN 63 and 97)
	GROUP BY PMP.txtBoardingHouse) AS '- Events', 

	(SELECT	SUM(RMRTC.intValue) AS exp4
	FROM	TblPupilManagementPupils AS PMP 
			INNER JOIN TblRewardsManagerRewards AS RMR ON PMP.txtSchoolID = RMR.txtSchoolID 
			INNER JOIN TblRewardsManagerRewardTypes AS RMRT 
			INNER JOIN TblRewardsManagerRewardTypesCategories AS RMRTC ON RMRT.TblRewardsManagerRewardTypesID = RMRTC.intType
						ON RMR.intCategory = RMRTC.TblRewardsManagerRewardTypesCategoriesID AND RMR.intType = RMRT.TblRewardsManagerRewardTypesID
	WHERE	(RMRT.intPositive = - 1) AND (PMP.txtSchoolID = RMR.txtSchoolID) AND (RMR.txtDate BETWEEN '2013-01-01' AND '2014-01-01') 
			AND (RMRT.TblRewardsManagerRewardTypesID BETWEEN 19 AND 24) AND (RMRTC.TblRewardsManagerRewardTypesCategoriesID BETWEEN 63 and 97)
	GROUP BY PMP.txtBoardingHouse) AS '- Points'

FROM	TblRewardsManagerRewards AS RMR
		INNER JOIN TblPupilManagementPupils AS PMP ON RMR.txtSchoolID = PMP.txtSchoolID 
		INNER JOIN TblRewardsManagerRewardTypes AS RMRT ON RMR.intType = RMRT.TblRewardsManagerRewardTypesID 
		INNER JOIN TblRewardsManagerRewardTypesCategories AS RMRTC ON RMR.intCategory = RMRTC.TblRewardsManagerRewardTypesCategoriesID
WHERE	(PMP.intNCYear BETWEEN 9 and 13) AND (RMRT.TblRewardsManagerRewardTypesID BETWEEN 19 AND 24) AND (RMRTC.TblRewardsManagerRewardTypesCategoriesID BETWEEN 63 and 97) 
		AND (PMP.txtForm IS NOT NULL) AND (RMR.txtDate BETWEEN '2013-01-01' AND '2014-01-01')

GROUP BY PMP.txtBoardingHouse
ORDER BY PMP.txtBoardingHouse
Posted
Updated 5-May-14 20:57pm
v2

1 solution

I believe it's possible to achieve in much simplest way:
SQL
SELECT	PMP.txtBoardingHouse AS House, RMRT.intPositive AS Descr, COUNT(RMRTC.intValue) AS EventsCount, SUM(RMRTC.intValue) AS ReachedPoints
FROM	TblPupilManagementPupils AS PMP 
	INNER JOIN TblRewardsManagerRewards AS RMR ON PMP.txtSchoolID = RMR.txtSchoolID 
	INNER JOIN TblRewardsManagerRewardTypes AS RMRT 
	INNER JOIN TblRewardsManagerRewardTypesCategories AS RMRTC 
                ON RMRT.TblRewardsManagerRewardTypesID = RMRTC.intType 
        	ON RMR.intCategory = RMRTC.TblRewardsManagerRewardTypesCategoriesID 
            AND RMR.intType = RMRT.TblRewardsManagerRewardTypesID
WHERE RMR.txtDate BETWEEN '2013-01-01' AND '2014-01-01' AND RMRT.TblRewardsManagerRewardTypesID BETWEEN 19 AND 24
      AND RMRTC.TblRewardsManagerRewardTypesCategoriesID BETWEEN 63 and 97
GROUP BY PMP.txtBoardingHouse, RMRT.intPositive 

Result should look like:
House    Descr  EventsCount    ReachedPoints
House 1   1      10             20
House 1  -1      10             10
House 2   1       5             10
House 2  -1       5              5


Now, you can 'convert' it to expected format:
SQL
SELECT House, SUM(CASE WHEN Descr =1 THEN EventCount ELSE NULL END) AS [+Events],
    SUM(CASE WHEN Descr =-1 THEN EventCount ELSE NULL END) AS [-Events],
    SUM(CASE WHEN Descr =1 THEN ReachedPoints ELSE NULL END) AS [+Points], 
    SUM(CASE WHEN Descr =-1 THEN ReachedPoints ELSE NULL END) AS [-Points]
FROM (
--above query
) AS T
GROUP BY House


Note: Do not use the same query as a subquery to produce several columns!
 
Share this answer
 
Comments
kswire87 6-May-14 9:31am    
Perfect. That is exactly what I needed!

Thank you very much. This is by far simpler and more effective.
Maciej Los 6-May-14 9:45am    
I'm glad to hear that ;)
You're welcome ;)
King Fisher 7-May-14 0:34am    
great Job
Maciej Los 7-May-14 1:38am    
Thank you ;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900