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 > Help with sorting strings...

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-01-05, 10:40
dfoo dfoo is offline
Registered User
 
Join Date: Mar 2005
Posts: 5
Help with sorting strings...

I'm trying to search a database and get a list of results of
the latest values which are of type 'STRING'. How would I do it?

For instance, I've got a dataset like the one below.

Col 1 Col 2 Col 3
------------------------------------------------
Dog Blue 11a
Dog Blue 11b
Cat Blue 14
Cat Red 21a
Cat Red 21b
Fish Yellow 31
Shark Black 12a
Shark Purple 21

I only want it to return the ones with the highest 'Col 3' value, so it returns something like.

Col 1 Col 2 Col 3
------------------------------------------------------
Dog Blue 11b
Cat Red 21b
Fish Yellow 31
Shark Purple 21

I've tried something like this:

SELECT
table.col1,
table.col2,
table.col3
FROM
table
WHERE
1 > (
SELECT
COUNT(DISTINCT table.col3)
FROM
table tab
WHERE
tab.col3 > table.col3
)

However I get the ERR: An aggregate may not appear in the WHERE clause
unless it is in a subquery contained in a HAVING clause or select
list, and the column being aggregated is an outer reference.
Reply With Quote
  #2 (permalink)  
Old 03-01-05, 11:21
gannet gannet is offline
Registered User
 
Join Date: Oct 2002
Location: Plymouth UK
Posts: 116
I don't what your backend database is but this should help.
Code:
SELECT t.col1
     , t.col2
     , t.col3
FROM tablex t
 , (SELECT tablex.col1
         , max (tablex.col3) col3
    FROM tablex
    group by col1) g
WHERE t.col1 = g.col1
AND t.col3 = g.col3
;
or using ANSI joins
Code:
SELECT t.col1
     , t.col2
     , t.col3
FROM tablex t
JOIN (SELECT tablex.col1
         , max (tablex.col3) col3
    FROM tablex
    group by col1) g
ON t.col1 = g.col1
AND t.col3 = g.col3
;
Reply With Quote
  #3 (permalink)  
Old 03-02-05, 04:43
dfoo dfoo is offline
Registered User
 
Join Date: Mar 2005
Posts: 5
Thanks for your help gannet.
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