Home > Enterprise >  How to make total after the count column values in SQL
How to make total after the count column values in SQL

Time:08-10

I try to find

select Code, count(Code) 
from Table 
group by code

and need result to look like this:

enter image description here

CodePudding user response:

You can do it as follows:

SELECT Code, count(Code) as count
FROM Table 
GROUP BY count

You just need to adjust your GROUP BY condition.

CodePudding user response:

You probably need to do this somewhere other than in SQL, but it still works to have the total column. If you want to work on the ordering to make sure that the TOTAL column is at the bottom. Add an ortdering column and order by that

select Code, count(Code) 
from Table 
group by code
UNION
select 'All' as Code, count(Code) as COUNT
from Table 
  •  Tags:  
  • sql
  • Related