IWETHEY v. 0.3.0 | TODO
1,095 registered users | 0 active users | 0 LpH | Statistics
Login | Create New User
IWETHEY Banner

Welcome to IWETHEY!

New If you're allowed correlated subquery, then:
\nSELECT a.*\n  FROM table_name aa,\n       (SELECT a, b, max(date)\n          FROM table_name\n         GROUP BY a, b) bb\n WHERE aa.a = bb.a\n   AND aa.b = bb.b\n   AND aa.date = bb.date\n

You shouldn't need more than one subquery to do this.

Edit: Sorry. I didn't see what you said about getting multiples of "a, b, date" back from the GROUP BY in the subquery. If that's the case, and you need to go to a variable number of tie-breakers, then you've effectively blown the limits of relational calculus.

IMO, your only choice at this point is to drop to a procedural language, stream the records by, and pick-out the ones you want to keep. Straight SQL simply will not work if you have *any* duplicate records. Even if each row is unique, nested subqueries will make your SELECT move like pondwater.


Second Edit:

Actually, combining with Ben's solution, you can do something like:

\nselect aa.*\n  from table aa,\n       (select a, b, \n               max(to_char(date)||c||d||e) sortkey\n          from table) bb\n where aa.a = bb.a\n   and aa.b = bb.b\n   and to_char(aa.date)||aa.c||aa.d||aa.e = bb.sortkey\n


Like I said above, though, if you have a duplicate row, then you're screwed. Unless, of course, you're using Oracle and can append the 'rowid' psuedocolumn to the sortkey field.
Expand Edited by morganek April 16, 2004, 11:33:52 PM EDT
Expand Edited by morganek April 16, 2004, 11:43:11 PM EDT
New Re: If you're allowed correlated subquery, then:
combining with Ben's solution, you can do something like:
\nselect aa.*\n  from table aa,\n       (select a, b, \n               max(to_char(date)||c||d||e) sortkey\n          from table) bb\n where aa.a = bb.a\n   and aa.b = bb.b\n   and to_char(aa.date)||aa.c||aa.d||aa.e = bb.sortkey\n


Thanks, I think that'll do it.

I didn't understand what Ben meant when I first read his post, but it looks clear in retrospect. I'm in the habit of thinking of data as discrete objects and didn't consider stringing their text together.
     Slow query needs optimizing - (tangaroa) - (19)
         Re: Slow query needs optimizing - (pwhysall) - (3)
             Just imagine a Beowolf cluster of those. -NT - (ChrisR) - (2)
                 Great, now we're turning into /. - (inthane-chan)
                 Running DOSiX! -NT - (Another Scott)
         Can you post - (jbrabeck) - (6)
             'fraid not - (tangaroa) - (5)
                 pump to file, cat file |sort -k3 >outputfile - (boxley)
                 If you're allowed correlated subquery, then: - (morganek) - (1)
                     Re: If you're allowed correlated subquery, then: - (tangaroa)
                 Re: 'fraid not - (JayMehaffey) - (1)
                     Is "SQL Sever" intentional? (Regardless, I like it. :-) -NT - (ben_tilly)
         I don't recognize the "within" clause - (ben_tilly)
         Not quite sure what you are doing - (JayMehaffey) - (6)
             Whew, glad it's not just me that didn't understand. -NT - (jbrabeck)
             Pet peeve on SQLServer - (ChrisR) - (4)
                 Rarely use real temporary tables - (JayMehaffey) - (3)
                     I just finished writing about 15 stored procedures. - (mmoffitt) - (2)
                         Table variables are great - (ChrisR) - (1)
                             Thanks. - (mmoffitt)

Crazy moon-lovers!
52 ms