Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Avoid Group By??

Author  Topic 

kcarbone1970
Yak Posting Veteran

52 Posts

Posted - 2015-04-03 : 12:55:40
Hello I have this query that I want to display only one row that contains the data, because of the Group By on "Subgroup ID" it displays many records with null values and within them are the values that I only want to show. I tried using MAX on my case statements but I get error "Cannot perform an aggregate function on an expression containing an aggregate or a subquery."


SELECT
CASE WHEN [Subgroup ID] = 1 THEN CAST((SUM (b) / SUM (a) * 100) AS DECIMAL (36,2)) END AS Overall_Math,
CASE WHEN [Subgroup ID] = 74 THEN CAST((SUM (b) / SUM (a) * 100) AS DECIMAL (36,2)) END AS AA_Math ,
CASE WHEN [Subgroup ID] = 80 THEN CAST((SUM (b) / SUM (a) * 100) AS DECIMAL (36,2)) END AS White_Math ,
CASE WHEN [Subgroup ID] = 76 THEN CAST((SUM (b) / SUM (a) * 100) AS DECIMAL (36,2)) END AS Asian_Math ,
CASE WHEN [Subgroup ID] = 78 THEN CAST((SUM (b) / SUM (a) * 100) AS DECIMAL (36,2)) END AS Hispanic_Math

FROM

(
SELECT [Subgroup ID],
SUM(CAST([Students with Scores] AS float)) AS a,
CAST([Students with Scores] AS float) *
CAST(CONVERT(float , (1.0 * CAST([Percentage AT OR Above Proficient] AS float) / 100)) AS VARCHAR) AS b

FROM CST_ALL
WHERE ( [District Code] = 64774 )
AND ( [Test Type] = 'C' )
AND ( [Test Id] IN ( 7 ) )
AND ([School Code] = 0000000)
AND Grade IN ( 2, 3, 4, 5, 6, 7, 8, 9, 10, 11)

GROUP BY [Subgroup ID] , [Students with Scores] ,
[Percentage At Or Above Proficient]
)

AS c

GROUP BY [Subgroup ID]


Cartesian Yak

kcarbone1970
Yak Posting Veteran

52 Posts

Posted - 2015-04-03 : 13:04:47
Thanks I think I got it!


SELECT
MAX(Overall) AS Overall,
MAX(AA) AS AA,
MAX(White) AS Whites,
MAX(Asian) AS Asians,
MAX(Hispanic) AS Hispanics

FROM
(
SELECT
CASE WHEN [Subgroup ID] = 1 THEN CAST((SUM (b) / SUM (a) * 100) AS DECIMAL (36,2)) END AS Overall,
CASE WHEN [Subgroup ID] = 74 THEN CAST((SUM (b) / SUM (a) * 100) AS DECIMAL (36,2)) END AS AA ,
CASE WHEN [Subgroup ID] = 80 THEN CAST((SUM (b) / SUM (a) * 100) AS DECIMAL (36,2)) END AS White ,
CASE WHEN [Subgroup ID] = 76 THEN CAST((SUM (b) / SUM (a) * 100) AS DECIMAL (36,2)) END AS Asian,
CASE WHEN [Subgroup ID] = 78 THEN CAST((SUM (b) / SUM (a) * 100) AS DECIMAL (36,2)) END AS Hispanic

FROM

(
SELECT [Subgroup ID],
SUM(CAST([Students with Scores] AS float)) AS a,
CAST([Students with Scores] AS float) *
CAST(CONVERT(float , (1.0 * CAST([Percentage AT OR Above Proficient] AS float) / 100)) AS VARCHAR) AS b

FROM CST_ALL
WHERE ( [District Code] = 64774 )
--( [District Code] LIKE CAST(@CDS AS VARCHAR(7)) + '%' )
AND ( [Test Type] = 'C' )
AND ( [Test Id] IN ( 7 ) )
AND ([School Code] = 0000000)
AND Grade IN ( 2, 3, 4, 5, 6, 7, 8, 9, 10, 11)

GROUP BY [Subgroup ID] , [Students with Scores] ,
[Percentage At Or Above Proficient]
)

AS c

GROUP BY [Subgroup ID]
)
AS d


Cartesian Yak
Go to Top of Page
   

- Advertisement -