Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Selecting one row per field

Status
Not open for further replies.

azzazzello

Technical User
Jan 25, 2005
297
US
Suppose I have a table consisting of 10 fields.

f1,f2,...,f10

f1 is not the primary key, and hence has repetitions

suppose I have

the following set of data

Code:
AAA,1,2,...
BBB,3,4,....
AAA,5,9,...
CCC,4,1,...
BBB,3,7,...

I need to return any ENTIRE row for a distinct value of f1.
So ideally my result would be

AAA,1,2,...
BBB,3,7,...
CCC,2,3,...

thous is can be ANY of the repeating rows, as long as there is just ONE in the resultant set
 
i just realized that I can just group by f1 in MySQL, however, unfortunately, my database is Oracle and does not allow having fields in the resultant set that are not in group. This forum having been most helpful in the past, I thought I would ask a generic SQL question here. Well, I will go ask in another forum I guess.
 
Standard SQL is that you have to use aggregate functions for the columns that you are not GROUPing by. So you usually use MIN or FIRST if oracle supports it.

+++ Despite being wrong in every important aspect, that is a very good analogy +++
Hex (in Darwin's Watch)
 
OK...but the problem is I want the first (or min or max or whatever - anything!) of every distinct f1. I am not sure how to get that...
 
Code:
SELECT f1
     , MIN(f2)
     , MAX(f3)
     , AVG(f4)
     , MAX(f5)
     , MIN(f6)
     , MAX(f7)
     , AVG(f8)
     , MIN(f9)
     , MAX(f10)
  FROM daTable
GROUP
    BY f1

r937.com | rudy.ca
Buy my new book Simply SQL from Amazon
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top