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--

Slightvw,
Your example seems to add add a new column, I need the total per column at the bottom of each column.

0

Is your CI/CD pipeline a hodge-podge of randomly connected tools? You’ve likely got a tool to fix one problem & then a different tool to fix another, resulting in a cluster of tools with overlapping functionality. Learn how to optimize your pipeline with Gartner's recommendations

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--

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--

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on.
…

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.