Oracle development languages
Home > Ask the Oracle Experts > SQL Questions & Answers > Columns in the SELECT not in the GROUP BY
Ask The Oracle Expert: Questions & Answers
EMAIL THIS

Columns in the SELECT not in the GROUP BY

Rudy Limeback EXPERT RESPONSE FROM: Rudy Limeback

Pose a Question
Other Oracle Categories
Meet all Oracle Experts
Become an Expert for this site


Oracle tips, scripts, and expert advice
Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google


>
QUESTION POSED ON: 26 April 2007

I have an SQL query where I am using the SUM function with the GROUP BY clause. Now what I require is that I need to select a couple of more columns from the table without adding those columns in the GROUP BY clause. Is there any way I can do this?


>
EXPERT RESPONSE

The answer is yes, but only in a very certain way.

Let's imagine that this is your current query:

select foo
     , sum(bar) as sumbar
  from qux
group
    by foo 

Now, we want to add two more columns—let's call them other1 and other2—but we don't want to add them to the GROUP BY, because we still want only one row per foo in the results.

The very certain and only way that this will be allowed is by ensuring that the two new columns are inside aggregate functions.

Here's one example of many possibilities:

select foo
     , sum(bar) as sumbar
     , max(other1) as maxcol1
     , avg(other2) as avgcol2
  from qux
group
    by foo 

What is NOT ALLOWED is to put "naked" columns (not inside aggregate functions) into the SELECT clause but leave them out of the GROUP BY clause:

select foo
     , sum(bar) as sumbar
     , other1
     , other2
  from qux
group
    by foo 

This is invalid syntax, and only MySQL will run it (although they do warn you, in GROUP BY and HAVING with Hidden Fields, that results can be unpredictable).


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google


RELATED CONTENT
SQL
Querying metadata and data at the same time
Correlated update in Oracle
How do I retrieve second transaction for each customer?
Entity-Attribute-Value constraints
SQL to update the header records?
A running total
CHECK constraints
SQL to round up to nearest thousand
Collapsing a one-to-many result set
Is this where we use a RIGHT OUTER JOIN?

Oracle SQL
SQL to count values of a status code
Counting NULL columns
Detail rows for accounts that occur three times
Counting a row's NULL columns
Oracle's free SQL Developer adds database migration tool
Latest transaction if no recent prior transactions
Three ways SQL can count rows by type
SQL to select only certain times within a date range
Oracle SQL to test for numerics
Number of rows in multiple tables
Oracle SQL Research

RELATED GLOSSARY TERMS
Terms from Whatis.com − the technology online dictionary
autonomous transaction  (SearchOracle.com)
CFML  (SearchOracle.com)
dynamic SQL  (SearchOracle.com)
foreign key  (SearchOracle.com)
Java Database Connectivity  (SearchOracle.com)
Open Database Connectivity  (SearchOracle.com)
Oracle  (SearchOracle.com)
stored procedure  (SearchOracle.com)
The Open Group  (SearchOracle.com)

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary



Search and Browse the Expert Answer Center
Search and browse more than 25,000 question and answer pairs from more than 250 TechTarget industry experts.
Browse our Expert Advice

HomeNewsTopicsTipsAsk the ExpertsWebcastsWhite PapersProductsBlogs
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides enterprise IT professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective IT purchase decisions and managing their organizations' IT projects - with its network of technology-specific Web sites, events and magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Reprints  |  Site Map




All Rights Reserved, Copyright 2003 - 2008, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts