Q
Problem solve Get help with specific problems with your technologies, process and projects.

When "grouped by" means ORDER BY

I have a table with the following fields and I want to retrieve the results in the following form. TName and TicketNo must be grouped by DateIssue. Since the "Group By" clause uses aggregate functions only, the following type of query will not run.

I have a table with the following fields:

TicketNo  varchar2
TName     varchar2
DateIssue date/time

I want to retrieve the results in the following form:

DateIssue      TName    TicketNo
-----------    -----    --------
01-oct-2006    ABC      123-7733
               DEF      545-5445
               GHI      254-4545
02-oct-2006    JKL      454-7897
               MNO      444-7878

TName and TicketNo must be grouped by DateIssue. Since the "Group By" clause uses aggregate functions only, the following type of query will not run:

Select DateIssue,TName,TicketNo 
from Table1 
group by DateIssue 
order by DateIssue

How to display the above given result?

There are two issues. First, GROUP BY does not necessarily require the use of aggregate functions, although most GROUP BY queries do use them. However, the most important thing about GROUP BY is that it collapses multiple rows into one. This isn't what you want here. You want all your detail rows, but just "grouped" or "arranged" by DateIssue. All you need here is ORDER BY.

select DateIssue
     , TName
     , TicketNo 
  from Table1 
order 
    by DateIssue

The second issue is what I call "prettification" of the results. This word comes from the verb "prettify," which means "to make superficially pretty."

The simple ORDER BY query will give the following results:

DateIssue      TName    TicketNo
-----------    -----    --------
01-oct-2006    ABC      123-7733
01-oct-2006    DEF      545-5445
01-oct-2006    GHI      254-4545
02-oct-2006    JKL      454-7897
02-oct-2006    MNO      444-7878

To "suppress" or "hide" the repetitions of the DateIssue value, process the query's result set in your application. This is not something to be attempted with SQL.

This was last published in February 2007

Dig Deeper on Oracle and SQL

PRO+

Content

Find more PRO+ content and other member only offers, here.

Have a question for an expert?

Please add a title for your question

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

Start the conversation

Send me notifications when other members comment.

By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Please create a username to comment.

-ADS BY GOOGLE

SearchDataManagement

SearchBusinessAnalytics

SearchSAP

SearchSQLServer

TheServerSide

SearchDataCenter

SearchContentManagement

SearchFinancialApplications

Close