Grouping Rows with GROUP BY

Duplicate Columns

If a column is used more than once in the SELECT clause it does not need to appear more than once in the GROUP BY clause.

select grp_a, upper(grp_a), count(*)
from   t
group by GRP_A
order by grp_a ;
 
GRP_A      UPPER(GRP_   COUNT(*)
---------- ---------- ----------
a1         A1                  5
a2         A2                  3
 

If we did include the same column two or more times in the GROUP BY clause it would return the same results as the query above.

select grp_a, upper(grp_a), count(*)
from   t
group by GRP_A, GRP_A
order by grp_a ;
 
GRP_A      UPPER(GRP_   COUNT(*)
---------- ---------- ----------
a1         A1                  5
a2         A2                  3
 

While there is no practical use for the latter syntax in the upcoming topic GROUP_ID we will see how duplicate columns in a GROUPING SETS clause do produce different results than a distinct column list.




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-13112.html]SQL Snippets: Grouping Rows with GROUP BY - Duplicate Columns[/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-13112.html">SQL Snippets: Grouping Rows with GROUP BY - Duplicate Columns</a>

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

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