Using the query below, I need to add additional totals per column at the bottom of each column. I've got the totals by row but can't figure out how to add totals by column as well.
Can anyone point me in the right direction? I'm stuck on this one. Thanks!!

select grade_level, COUNT(CASE WHEN SCHOOLID = '1' THEN 1 end) School1,COUNT(CASE WHEN SCHOOLID = '2' THEN 1 end) School2,COUNT(CASE WHEN SCHOOLID = '3' THEN 1 end) School3,COUNT(CASE WHEN SCHOOLID = '4' THEN 1 end) School4,COUNT(*) "Total by Grade"from studentswhere enroll_status=0 group by grade_levelorder by grade_level

select grade_level, school1, school2, school3, school4, school1+school2+school3+school4 school_totalfrom (select grade_level, COUNT(CASE WHEN SCHOOLID = '1' THEN 1 end) School1,COUNT(CASE WHEN SCHOOLID = '2' THEN 1 end) School2,COUNT(CASE WHEN SCHOOLID = '3' THEN 1 end) School3,COUNT(CASE WHEN SCHOOLID = '4' THEN 1 end) School4from studentswhere enroll_status=0 group by grade_levelorder by grade_level)

------select grade_level, SUM(CASE WHEN SCHOOLID = '1' THEN 1 end) school1,SUM(CASE WHEN SCHOOLID = '2' THEN 1 end) school2,SUM(CASE WHEN SCHOOLID = '3' THEN 1 end) school3,SUM(CASE WHEN SCHOOLID = '4' THEN 1 end) school4,SUM(CASE WHEN SCHOOLID = '1' THEN 1 end) +SUM(CASE WHEN SCHOOLID = '2' THEN 1 end) +SUM(CASE WHEN SCHOOLID = '3' THEN 1 end) +SUM(CASE WHEN SCHOOLID = '4' THEN 1 end) school_totalfrom studentswhere enroll_status=0 group by grade_levelorder by grade_level--

I do need an example if possible. I've tried rollup in the group by clause but it totally messed up the query results, probably because I wasn't using it properly. Thanks!

Can you try this. Now could you please give a exact output you need as per this data. If possible provide in a tabular format or may be in excel.(Attach)

--CREATE TABLE tabless( Grade_Level INT ,SCHOOLID SmallINT ,enroll_status TinyINT)GOINSERT INTO tablessVALUES(1,1,0),(1,2,0),(1,3,0),(1,4,0),(1,2,0),(1,3,0),(1,1,0),(1,1,0),(1,1,0),(1,2,0),(1,3,0),(1,4,1)GOSELECT * FROM tablessselect CASE WHEN grade_level IS NULL Then 'Total' ELSE CAST(grade_level AS sql_variant) END grade_level, SUM(CASE WHEN SCHOOLID = '1' THEN 1 end) school1,SUM(CASE WHEN SCHOOLID = '2' THEN 1 end) school2,SUM(CASE WHEN SCHOOLID = '3' THEN 1 end) school3,SUM(CASE WHEN SCHOOLID = '4' THEN 1 end) school4from tablessgroup by grade_levelWITH Cube--

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

I need to change the schoolid's on my end to different numbers. Do I change the numbers in the values section too? Sorry, this is the first time I've seen a query like that. The rollup command won't work in this case?

--SELECT grade_level,SchoolId,TotalSchoolIdsFROM (SELECT CASE WHEN grade_level IS NULL Then 'Total' ELSE CAST(grade_level AS sql_variant) END grade_level, SCHOOLID SchoolId ,SUM(SCHOOLID) AS TotalSchoolIds FROM tabless WHERE enroll_status=0 GROUP BY ROLLUP(grade_level,SCHOOLID) ) t--

Not sure how I messed it up the first time but I figured it out using my original query.

select grade_level, COUNT(CASE WHEN SCHOOLID = '1' THEN 1 end) School1,COUNT(CASE WHEN SCHOOLID = '2' THEN 1 end) School2,COUNT(CASE WHEN SCHOOLID = '3' THEN 1 end) School3,COUNT(CASE WHEN SCHOOLID = '4' THEN 1 end) School4,COUNT(*) "Total by Grade"from studentswhere enroll_status=0 group by rollup(grade_level)order by grade_level

compute sum of school1 on report
compute sum of school2 on report
compute sum of school3 on report
compute sum of school4 on report
compute sum of totals on report
break on report
select grade_level,
COUNT(CASE WHEN SCHOOLID = '1' THEN 1 end) School1,
COUNT(CASE WHEN SCHOOLID = '2' THEN 1 end) School2,
COUNT(CASE WHEN SCHOOLID = '3' THEN 1 end) School3,
COUNT(CASE WHEN SCHOOLID = '4' THEN 1 end) School4,
count(*) totals
from students
where enroll_status=0
group by grade_level
order by grade_level;

I don't care about the points but you might want to take a look at my solution. There always seem to be more ways than one to reach a solution. For example, rather than the count(case... method, you could also use sum(decode, schoolid, 1, 1, 0) as school1, etc.

@Author - I have informed you about the grouping functions

"
Ohhh in that case you can use ROLLUP, CUBE, GROUPING Functions.

Pls let me know if you need example on this.

"

@Author - I have also given you the example of rollup...

"

--SELECT grade_level,SchoolId,TotalSchoolIdsFROM (SELECT CASE WHEN grade_level IS NULL Then 'Total' ELSE CAST(grade_level AS sql_variant) END grade_level, SCHOOLID SchoolId ,SUM(SCHOOLID) AS TotalSchoolIds FROM tabless WHERE enroll_status=0 GROUP BY ROLLUP(grade_level,SCHOOLID) ) t--

just added rollup() to the group by clause of my original query

0

Featured Post

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Cursors in Oracle:
A cursor is used to process individual rows returned by database system for a query.
In oracle every SQL statement executed by the oracle server has a private area. This area contains information about the SQL statement and the…

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…

This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.