Grouping Rows with GROUP BY

SELECT Lists

We may group by table columns that are not in the SELECT list, like GRP_B in the example below.

select grp_a, count(*)
from   t
group by grp_a, GRP_B
order by grp_a, grp_b ;
 
GRP_A    COUNT(*)
------ ----------
a1              2
a1              3
a2              3
 

However we may not select table columns that are absent from the GROUP BY list, as with GRP_A in this example.

select GRP_A, count(*)
from   t
GROUP BY GRP_B ;
select GRP_A, count(*)
       *
ERROR at line 1:
ORA-00979: not a GROUP BY expression


 



Linking to SQL Snippets ™

To link to this page in Oracle Technology Network Forums or OraFAQ Forums cut and paste this code.

  • [url=http://www.sqlsnippets.com/en/topic-13114.html]SQL Snippets: Grouping Rows with GROUP BY - SELECT Lists[/url]

To link to this page in HTML documents or Blogger comments cut and paste this code.

  • <a href="http://www.sqlsnippets.com/en/topic-13114.html">SQL Snippets: Grouping Rows with GROUP BY - SELECT Lists</a>

To link to this page in other web sites use the following values.

  • Link Text : SQL Snippets: Grouping Rows with GROUP BY - SELECT Lists
  • URL (href): http://www.sqlsnippets.com/en/topic-13114.html