If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > Alternative to GROUP BY

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-23-04, 20:32
Nerddette Nerddette is offline
Registered User
 
Join Date: Apr 2004
Location: Australia
Posts: 7
Alternative to GROUP BY

I have another question from my Uni assignment! I'm sure it's the way the questions are written that I'm having difficulty with!

==============================
Write a query with the same meaning as the following query, but does not use a GROUP BY clause.

SELECT EventId, MIN (ElapsedTime), MAX (ElapsedTime)
FROM Results
GROUP BY EventId
=============================

There are approx 8 results per event, each with an elapsed time - it is results of swimming races.

I thought you had to use a GROUP BY with aggregate functions? I thought about temporary tables, but that doesn't eliminate the group by.

Thanks!

Nerddette
Reply With Quote
  #2 (permalink)  
Old 05-23-04, 22:18
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
what a wonderful question!!

have you tried a three-way self join with two correlated subqueries?

it will give the right answer, but it's not very efficient!!
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 05-23-04, 23:17
derrickleggett derrickleggett is offline
Registered User
 
Join Date: Apr 2004
Location: Kansas City, MO
Posts: 734
Tell your teacher he/she is an idiot and the query works perfectly well the way it's written. (grin)
__________________
MeanOldDBA
derrickleggett@hotmail.com
When life gives you a lemon, fire the DBA.
Reply With Quote
  #4 (permalink)  
Old 05-23-04, 23:36
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
derrick, it was an intellectual exercise

whatsamatter, you couldn't do it?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 05-24-04, 01:36
derrickleggett derrickleggett is offline
Registered User
 
Join Date: Apr 2004
Location: Kansas City, MO
Posts: 734
No, I couldn't do it because it's stupid. I'm allergic to doing something stupid for intellectual reasons.
__________________
MeanOldDBA
derrickleggett@hotmail.com
When life gives you a lemon, fire the DBA.
Reply With Quote
  #6 (permalink)  
Old 05-24-04, 01:41
Nerddette Nerddette is offline
Registered User
 
Join Date: Apr 2004
Location: Australia
Posts: 7
Quote:
Originally Posted by r937
whatsamatter, you couldn't do it?
* puts her hand up * Um... yes? I still can't do it!

Any ideas, any commands that I can look up the syntax for? I think it might involve a subquery in the SELECT list, but I'm not sure.

Thanks.

Nerddette
Reply With Quote
  #7 (permalink)  
Old 05-24-04, 01:58
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
nerddette, i gave you my idea in post #2

i tested it and it works
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #8 (permalink)  
Old 05-24-04, 02:05
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,605
Without knowing which version of what database engine you are using, it is kind of tough to forumuate an answer. Assuming that your particular engine supports at least the basic SQL-92 syntax, you could do a SELECT DISTINCT to get the grouping done for you (it does the same thing, but doesn't require the GROUP BY clause). Once you've got that, you can probably use correlated sub-queries to get the Min and Max values for that particular EventId.

If you are looking for "pre-cooked" SQL, ready to submit for a grade, you will probably wait a long time here. I'm always happy to help somebody out, but I'm allergic to doing other people's homework!

I think that Rudy is just being perverse with the three-way self-join. I can see how it would work (although I wouldn't want to actually watch it), but I can think of at least 1e2 (an inside joke) easier ways to do it!

-PatP
Reply With Quote
  #9 (permalink)  
Old 05-24-04, 02:41
Nerddette Nerddette is offline
Registered User
 
Join Date: Apr 2004
Location: Australia
Posts: 7
Quote:
Originally Posted by Pat Phelan
If you are looking for "pre-cooked" SQL, ready to submit for a grade, you will probably wait a long time here. I'm always happy to help somebody out, but I'm allergic to doing other people's homework!
Thanks for your help. I'm not looking for "pre-cooked" answers, but I understand your concerns as I get students at Uni wanting the same from me.

Nerddette
Reply With Quote
  #10 (permalink)  
Old 05-24-04, 07:39
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Quote:
Originally Posted by Pat Phelan
I think that Rudy is just being perverse with the three-way self-join.
nope

it was the only way i could think of to do it

i do not, however, see how your way would work, pat

a correlated subquery can be used to restrict which value of ElapsedTime is chosen, but how do you get both min and max selected without a self-join?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #11 (permalink)  
Old 05-24-04, 11:31
sushant sushant is offline
Registered User
 
Join Date: Jan 2004
Posts: 49
Folks,

How about following

select distinct EventId
,min_elapsed=(select min(b.ElapsedTime)
from Results c
where b.EventId=a.EventId)
,max_elapsed=(select max(c.ElapsedTime)
from Results c
where c.EventId=a.EventId)
from Results a
Reply With Quote
  #12 (permalink)  
Old 05-24-04, 11:54
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,605
Quote:
Originally Posted by r937
a correlated subquery can be used to restrict which value of ElapsedTime is chosen, but how do you get both min and max selected without a self-join?
Sushant is one character away from what I intended.

-PatP
Reply With Quote
  #13 (permalink)  
Old 05-24-04, 12:01
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
sushant, that's eventually where my self-join was headed

pat's right, i was being sneaky (i am trying to learn from a master)

i wasn't just going to plop orthogonality into the discussion without some sort of leadup

by the way you have a syntax error, or was that on purpose too? (just kidding)
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #14 (permalink)  
Old 05-24-04, 12:05
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,605
Quote:
Originally Posted by r937
i wasn't just going to plop orthogonality into the discussion without some sort of leadup
Awwww, why not ?!?!

Sneaky? Is there somebody being sneaky around here? Who, where, how ??? Why am I always the last one to find out about these things ?!?!

-PatP
Reply With Quote
  #15 (permalink)  
Old 05-24-04, 12:20
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
speaking of orthogonality, i went in search of a few good links, and look what i found:

RelationalWeenie

two things of interest there: the entire wiki looks like a goldmine for computer related stuff, and look, it's another site with a two-character domain name -- do you have any idea what that domain name might be worth on the open market? and there i was, at the dawn of the web, when there were plenty of these names available, and i never bothered to snap a few up...
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On